You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please, add feature with adding custom attribute for viewmodel with custom validation logic.
CustomValidationAttribute should have:
bool IsValid method with Value parameter (type object)
property string ErrorMessage to response custom error
publicclassFlightDateValidationAttribute:ValidationAttribute{publicnewstringErrorMessage="Flight date is invalid";// orpublicoverridestringMessage(){return"Flight date is invalid";}// up to youpublicoverrideboolIsValid(objectvalue){vardate= value asstring;return DateTime.TryParse(date,outvar flightDate)&& flightDate.Date >= DateTime.UtcNow.Date;}}
/// <summary>/// Indicates that this property should be a valid email address/// </summary>[AttributeUsage(AttributeTargets.Property)]publicclassEMailAttribute:ValidationAttribute{/// <summary>/// Initializes a new instance of the <see cref="EMailAttribute"/> class./// </summary>/// <param name="errorMessage">The custom error message, should contain string table item key if 'isMessageFromStringTable' is true.</param>/// <param name="isMessageFromStringTable">if set to <c>true</c> then indicates that errorMessage is containing string table item key instead of string error message.</param>publicEMailAttribute(stringerrorMessage=null,boolisMessageFromStringTable=true):base(errorMessage, isMessageFromStringTable){}/// <summary>/// Validates the specified property value./// </summary>/// <param name="value">The object value.</param>/// <param name="propertyInfo">Information about the property containing this attribute.</param>/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>publicoverridevoidValidate(objectvalue,PropertyInfopropertyInfo,IDIResolverresolver){if(!(value isstring))return;if(StringHelper.ValidateEMail((string)value))return;
TryThrowCustomOrStringTableException(resolver);thrownew ModelValidationException($"Property '{propertyInfo.Name}' should be an email, actual value: '{value}'");}}
Please, add feature with adding custom attribute for viewmodel with custom validation logic.
CustomValidationAttribute should have:
In ViewModel:
or
The text was updated successfully, but these errors were encountered: