jwaliszko / ExpressiveAnnotations

Annotation-based conditional validation library.
MIT License
351 stars 123 forks source link

RequiredIf - Remove multiple validation messages in realtime #36

Closed Luciano74 closed 9 years ago

Luciano74 commented 9 years ago

Hi, just started using your impressive library, and have a question regarding the RequiredIf validatior.

Assuming a MVC5 form with a model with 2 properties:

[RequiredIf("LastName == null", ErrorMessage = "At least first or last name should be provided.")] public string FirstName{ get; set; }

[RequiredIf("FirstName == null", ErrorMessage = "At least first or last name should be provided.")] public string LastName { get; set; }

  1. With empty textboxes, when we click submit we get this. 1
  2. When we type in one of the textboxes the validation message under that textbox disappears. 2
  3. My question: How would we do to also make the second validation message disappear in realtime, when we type in the first textbox (or vice versa)? Like in the image below. 3

Right now, to make the second validation message to dissapear, I have to 1.) Place the pointer in the second textbox. 2.) Remove the pointer from the second textbox.

jwaliszko commented 9 years ago

Hi,

jQuery Validation validates a field, when this particular field has changed. Nevertheless, your notice is fully justified - when using ExpressiveAnnotations, since the state of a field can relay on any other fields, the validation outcome should be extended.

Currently, there is no built-in support. But you can simply overcome this inconvenience and trigger entire form validation whenever you want, by adding this piece of code to your page:

<script>
    $(document).ready(function() {
        $('form input, form select').bind('change paste keyup', function () {
            $('form').valid();
        });
    }); 
</script>

This will re-validate all the fields (bear in mind that you can narrow that binding to your particular form and selected fields, since I've bind to all input and select fields, which may be simply too much).

When it comes to EA, ideally, it should trigger validation for every related field. I'll take a look at this.

Regards

jwaliszko commented 9 years ago

I've just implemented this exact functionality into expressive.annotations.validate.js script. It's not yet published. If you want, get this script, include to your project and refresh the cache. Finally, you have to turn this function on, by this line: ea.settings.instantValidation = true;.

The best solution which I'd like to have, mainly firing validation for the related fields only and by default, is not yet there.

Luciano74 commented 9 years ago

Wow, that was quick, this is really nice.

jwaliszko commented 9 years ago

I've just figured out how to trigger validation, directly for such fields, which state depends on a field which is currently changed. It is by default now. You can turn off instantValidation if you don't need every field to be revalidated.

Modified script (expressive.annotations.validate.js) is pushed to the master branch. I'll publish it within next days (if you find any problem or something which could be further improved, just give me a hint).