// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq.Expressions;
using Microsoft.AspNetCore.Html;
namespace Microsoft.AspNetCore.Mvc.Rendering
{
///
/// Validation-related extensions for and .
///
public static class HtmlHelperValidationExtensions
{
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// A new containing a element.
/// An empty if the is valid and client-side
/// validation is disabled.
///
///
/// Method extracts an error string from the object. Message
/// will always be visible but client-side validation may update the associated CSS class.
///
public static IHtmlContent ValidationMessage(
this IHtmlHelper htmlHelper,
string expression)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationMessage(expression, message: null, htmlAttributes: null, tag: null);
}
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// The message to be displayed. If null or empty, method extracts an error string from the
/// object. Message will always be visible but client-side
/// validation may update the associated CSS class.
///
///
/// A new containing a element.
/// An empty if the is valid and client-side
/// validation is disabled.
///
public static IHtmlContent ValidationMessage(
this IHtmlHelper htmlHelper,
string expression,
string message)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationMessage(expression, message, htmlAttributes: null, tag: null);
}
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// An that contains the HTML attributes for the
/// () element. Alternatively, an
/// instance containing the HTML
/// attributes.
///
///
/// A new containing a element.
/// An empty if the is valid and client-side
/// validation is disabled.
///
///
/// Method extracts an error string from the object. Message
/// will always be visible but client-side validation may update the associated CSS class.
///
public static IHtmlContent ValidationMessage(
this IHtmlHelper htmlHelper,
string expression,
object htmlAttributes)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationMessage(expression, message: null, htmlAttributes: htmlAttributes, tag: null);
}
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// The message to be displayed. If null or empty, method extracts an error string from the
/// object. Message will always be visible but client-side
/// validation may update the associated CSS class.
///
///
/// The tag to wrap the in the generated HTML. Its default value is
/// .
///
///
/// A new containing a element. An empty
/// if the is valid and client-side validation is
/// disabled.
///
public static IHtmlContent ValidationMessage(
this IHtmlHelper htmlHelper,
string expression,
string message,
string tag)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationMessage(expression, message, htmlAttributes: null, tag: tag);
}
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// The message to be displayed. If null or empty, method extracts an error string from the
/// object. Message will always be visible but client-side
/// validation may update the associated CSS class.
///
///
/// An that contains the HTML attributes for the
/// () element. Alternatively, an
/// instance containing the HTML
/// attributes.
///
///
/// A new containing a element.
/// An empty if the is valid and client-side
/// validation is disabled.
///
public static IHtmlContent ValidationMessage(
this IHtmlHelper htmlHelper,
string expression,
string message,
object htmlAttributes)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationMessage(expression, message, htmlAttributes, tag: null);
}
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
/// The type of the model.
/// The type of the result.
///
/// A new containing a element.
/// An empty if the is valid and client-side
/// validation is disabled.
///
///
/// Method extracts an error string from the object. Message
/// will always be visible but client-side validation may update the associated CSS class.
///
public static IHtmlContent ValidationMessageFor(
this IHtmlHelper htmlHelper,
Expression> expression)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.ValidationMessageFor(expression, message: null, htmlAttributes: null, tag: null);
}
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
///
/// The message to be displayed. If null or empty, method extracts an error string from the
/// object. Message will always be visible but client-side
/// validation may update the associated CSS class.
///
/// The type of the model.
/// The type of the result.
///
/// A new containing a element.
/// An empty if the is valid and client-side
/// validation is disabled.
///
public static IHtmlContent ValidationMessageFor(
this IHtmlHelper htmlHelper,
Expression> expression,
string message)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes: null, tag: null);
}
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
///
/// The message to be displayed. If null or empty, method extracts an error string from the
/// object. Message will always be visible but client-side
/// validation may update the associated CSS class.
///
///
/// An that contains the HTML attributes for the
/// () element. Alternatively, an
/// instance containing the HTML
/// attributes.
///
/// The type of the model.
/// The type of the result.
///
/// A new containing a element.
/// An empty if the is valid and client-side
/// validation is disabled.
///
public static IHtmlContent ValidationMessageFor(
this IHtmlHelper htmlHelper,
Expression> expression,
string message,
object htmlAttributes)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes, tag: null);
}
///
/// Returns the validation message if an error exists in the
/// object for the specified .
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
///
/// The message to be displayed. If null or empty, method extracts an error string from the
/// object. Message will always be visible but client-side
/// validation may update the associated CSS class.
///
///
/// The tag to wrap the in the generated HTML. Its default value is
/// .
///
/// The type of the model.
/// The type of the result.
///
/// A new containing the element. An empty
/// if the is valid and client-side validation is
/// disabled.
///
public static IHtmlContent ValidationMessageFor(
this IHtmlHelper htmlHelper,
Expression> expression,
string message,
string tag)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.ValidationMessageFor(expression, message, htmlAttributes: null, tag: tag);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
///
/// New containing a <div> element wrapping the <ul> element.
/// An empty if the current model is valid and client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(this IHtmlHelper htmlHelper)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(
excludePropertyErrors: false,
message: null,
htmlAttributes: null,
tag: null);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
///
/// If true, display model-level errors only; otherwise display all errors.
///
///
/// New containing a <div> element wrapping the <ul> element.
/// An empty if the current model is valid and client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(this IHtmlHelper htmlHelper, bool excludePropertyErrors)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(
excludePropertyErrors,
message: null,
htmlAttributes: null,
tag: null);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
/// The message to display with the validation summary.
///
/// New containing a <div> element wrapping the
/// element (which wraps the
/// ) and the <ul> element. An empty if the current
/// model is valid and client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(this IHtmlHelper htmlHelper, string message)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(
excludePropertyErrors: false,
message: message,
htmlAttributes: null,
tag: null);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
/// The message to display with the validation summary.
///
/// The tag to wrap the in the generated HTML. Its default value is
/// .
///
///
/// New containing a <div> element wrapping the element
/// and the <ul> element. An empty if the current model is valid and
/// client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(this IHtmlHelper htmlHelper, string message, string tag)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(
excludePropertyErrors: false,
message: message,
htmlAttributes: null,
tag: tag);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
///
/// If true, display model-level errors only; otherwise display all errors.
///
/// The message to display with the validation summary.
///
/// New containing a <div> element wrapping the
/// element (which, in turn, wraps the
/// ) and the <ul> element. An empty if the current
/// model is valid and client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(
this IHtmlHelper htmlHelper,
bool excludePropertyErrors,
string message)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(
excludePropertyErrors,
message,
htmlAttributes: null,
tag: null);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
/// The message to display with the validation summary.
///
/// An that contains the HTML attributes for the topmost (<div>) element.
/// Alternatively, an instance containing
/// the HTML attributes.
///
///
/// New containing a <div> element wrapping the
/// element (which wraps the
/// ) and the <ul> element. An empty if the current
/// model is valid and client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(
this IHtmlHelper htmlHelper,
string message,
object htmlAttributes)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(
excludePropertyErrors: false,
message: message,
htmlAttributes: htmlAttributes,
tag: null);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
/// The message to display with the validation summary.
///
/// An that contains the HTML attributes for the topmost (<div>) element.
/// Alternatively, an instance containing
/// the HTML attributes.
///
///
/// The tag to wrap the in the generated HTML. Its default value is
/// .
///
///
/// New containing a <div> element wrapping the element
/// and the <ul> element. An empty if the current model is valid and
/// client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(
this IHtmlHelper htmlHelper,
string message,
object htmlAttributes,
string tag)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(
excludePropertyErrors: false,
message: message,
htmlAttributes: htmlAttributes,
tag: tag);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
///
/// If true, display model-level errors only; otherwise display all errors.
///
/// The message to display with the validation summary.
///
/// The tag to wrap the in the generated HTML. Its default value is
/// .
///
///
/// New containing a <div> element wrapping the element
/// and the <ul> element. An empty if the current model is valid and
/// client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(
this IHtmlHelper htmlHelper,
bool excludePropertyErrors,
string message,
string tag)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(
excludePropertyErrors,
message,
htmlAttributes: null,
tag: tag);
}
///
/// Returns an unordered list (<ul> element) of validation messages that are in the
/// object.
///
/// The instance this method extends.
///
/// If true, display model-level errors only; otherwise display all errors.
///
/// The message to display with the validation summary.
///
/// An that contains the HTML attributes for the topmost (<div>) element.
/// Alternatively, an instance containing
/// the HTML attributes.
///
///
/// New containing a <div> element wrapping the
/// element (which wraps the
/// ) and the <ul> element. An empty if the current
/// model is valid and client-side validation is disabled.
///
public static IHtmlContent ValidationSummary(
this IHtmlHelper htmlHelper,
bool excludePropertyErrors,
string message,
object htmlAttributes)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ValidationSummary(excludePropertyErrors, message, htmlAttributes, tag: null);
}
}
}