Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom ViewModel attribute with own validation logic #14

Closed
amanbaevsayat opened this issue Oct 8, 2019 · 1 comment
Closed

Custom ViewModel attribute with own validation logic #14

amanbaevsayat opened this issue Oct 8, 2019 · 1 comment
Assignees
Labels
Milestone

Comments

@amanbaevsayat
Copy link

amanbaevsayat commented Oct 8, 2019

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
public class FlightDateValidationAttribute : ValidationAttribute
    {
        public new string ErrorMessage = "Flight date is invalid";
        // or
        public override string Message(){
                  return "Flight date is invalid"; 
        }
        // up to you
        public override bool IsValid(object value)
        {
            var date = value as string;
            return DateTime.TryParse(date, out var flightDate) && flightDate.Date >= DateTime.UtcNow.Date;
        }
    }

In ViewModel:

[FlightDateValidation]
public DateTime FlightDate { get; set; }

or

[FlightDateValidation(ErrorMessage = "Flight date is invalid")] // if message is custom
public DateTime FlightDate { get; set; }
@i4004 i4004 self-assigned this Oct 8, 2019
@i4004 i4004 added this to the Version 3.0 milestone Oct 14, 2019
@i4004
Copy link
Member

i4004 commented Oct 17, 2019

Example:

/// <summary>
/// Indicates that this property should be a valid email address
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class EMailAttribute : 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>
	public EMailAttribute(string errorMessage = null, bool isMessageFromStringTable = 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>
	public override void Validate(object value, PropertyInfo propertyInfo, IDIResolver resolver)
	{
		if (!(value is string))
			return;

		if (StringHelper.ValidateEMail((string)value))
			return;

		TryThrowCustomOrStringTableException(resolver);

		throw new ModelValidationException($"Property '{propertyInfo.Name}' should be an email, actual value: '{value}'");
	}
}

@i4004 i4004 closed this as completed Oct 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants