0

A custom validation attribute in Asp.net core can be implemented by creating a class that implements ValidationAttribute, IClientModelValidator.

In that class one can code the validation rule and emmit the relevant data-val attributes.

On the JavaScript side, we need a function that performs the client side validation.

This can be archived by using .validator.addMethod(...) and $.validator.unobtrusive.adapters.add(...)

This code usually is added to the relevant .js file.

However, I would prefer to emit the javascript code from within my validation attribute. This would make sharing and reusing the attribute much easier.

Any ideas how to achive this?

4
  • You cannot. A ValidationAttribute is not responsible for (and cannot) generate html. That is the responsibility of HtmlHelper or TagHelper methods which read the metadata generated by the attribute. You could obviously write your own HtmlHelper extension methods or TagHelpers to emit script tags along with the html for the form control, but that would be a really bad idea (resulting in in-line scripts, duplicate scripts, scripts in the wrong order etc) Commented Jan 23, 2018 at 23:18
  • @StephenMuecke: I think that it is not 100% correct to say that ValidationAttributes cannot generate html. Currently they are already generating html attributes (data-val-x) inside the AddValidation method. However I understand that this is at the moment limited to attributes. Commented Jan 24, 2018 at 8:42
  • It is correct. Its the HtmlHelper or TagHelper methods that generate the html. For example the HtmlHelper class has a GetUnobtrusiveValidationAttributes() method which reads the metadata of the Validation attributes applied to a property to generate the data-val-* attributes. A ValidationAttribute has no context of the type of app its being used in - it could just as well be a Windows Form app. Commented Jan 24, 2018 at 8:52
  • Yes you are right. Technically it is the HtmlHelper that generates the output. What I was trying to say is that within the attribute class I can specify the attributes which the html helper will create. Nevertheless even if it would be possibly to do the same for javascript this still leads to the problems (inline scripts etc) which you pointed out. I was just unhappy that the validation code for client/server is in two different places but it seems I have to live with that. Commented Jan 24, 2018 at 10:03

1 Answer 1

1

There is a new framework called Dryv (from DRY Validation) for exactly this purpose. You can find it under https://dryv-lib.net. You can define your validation rules with lambda expressions in C# and they get translated into JavaScript.

public class Customer
{
    public static readonly DryvRules Rules = DryvRules
        .For<Customer>()
        .Rule(m => m.TaxId,
            m => string.IsNullOrWhiteSpace(m.Company) || !string.IsNullOrWhiteSpace(m.TaxId)
                ? DryvResult.Success
                : $"The tax ID for {m.Company} must be specified.");

    public string Company { get; set; }

    [Required]
    public string Name { get; set; }

    [DryvRules]
    public string TaxId { get; set; }
}

Disclaimer: I am the author of that framework.

Sign up to request clarification or add additional context in comments.

Comments

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.