1

Please ask if you can't understand what I'm asking.

I have created a custom ValidateAttribute for my ViewModel

i created it for validate properties which depend from another property of ViewModel

if (user checked "01" or "09" from QrupList) Then

  1. Company name is needed
  2. Name,surname and LastName are not needed

else

  1. Company name is not needed
  2. Name,surname and LastName are needed

I have ViewModel as below

[ValidateForGroupAttribute("Group", "CompanyName")]
public partial class AbonentViewModel
{      
    [DisplayName("Şirkət")]
    public string CompanyName { get; set; }

    [DisplayName("Soyadı")]
    [Required(ErrorMessage = "Soyadı vacibdir")]
    public string Surname { get; set; }      

    [DisplayName("Qrup")]
    [Required(ErrorMessage = "Qrup vacibdir")]
    public string Group{ get; set; }

    public SelectList GroupList { get; set; }
}

My custom ValidationAttribute classes:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class ValidateForGroupAttribute : ValidationAttribute
{
    private const string _defaultErrorMessage = "'{0}' a müvafiq '{1}' daxil din";

    public ValidateForGroupAttribute(string originalProperty, string confirmPropertyCompany)
        : base(_defaultErrorMessage)
    {
        OriginalProperty = originalProperty;
        ConfirmPropertyCompany = confirmPropertyCompany;
    }

    public string OriginalProperty { get; private set; }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
            OriginalProperty, ConfirmPropertyCompany);
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        object originalValue = properties.Find(OriginalProperty, true).GetValue(value);
        object confirmValueCompany = properties.Find(ConfirmPropertyCompany, true).GetValue(value);

        if ((string)originalValue == "01" || (string)originalValue == "09")
            return false;
        else
            return true;
    }
}

How do I do it? What is wrong in my ValidationAttributes?

0

1 Answer 1

2

We looked at validation using data annotations a few months back, and decided it was better to use something like fluent validation, as we had complex business rules and logic that would have taken too much effort to realise with data annotations. Have a look at the documentation, and you will see fluent validation makes things like this easy.

Sorry, I did not get back sooner: Check fluent validation here

Your rule could look something like. Syntax not tested, but I am sure you will be able to figure it out.

public class AbonentViewModelValidator : AbstractValidator<AbonentViewModel> {
    public AbonentViewModelValidator() {
        RuleFor(model => model.CompanyName).NotEmpty().When(model => (model.GroupList.Id == 1 || model.GroupList.Id == 9 ));
        RuleFor(model => model.Surname).NotEmpty().When(model => (model.GroupList.Id != 1 || model.GroupList.Id != 9 ));
        RuleFor(model => model.Name).NotEmpty().When(model => (model.GroupList.Id != 1 || model.GroupList.Id != 9 ));
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

can you give me some link for documentation?
thanks i will test it. i have done it with another way. Some cheks are making by DataAnnotation after it by my simple validator in controller. but i will test your code too. i do not liked my way yet:-).
it is good library.but do not want use it. maybe another time i will use it. Thanks for your response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.