jwaliszko / ExpressiveAnnotations

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

RequiredIf Client Validation on Form Load #68

Closed speshulk926 closed 9 years ago

speshulk926 commented 9 years ago

I have 2 fields that on form load automatically trigger the RequiredIf routine I have set up on page load even when I send in a null model. The problem is that AddressOutsideUS is False by default. All I really want is if the checkbox is checked, to not require the City and Zip (both which are showing invalid on page load)

Is there a way to keep it from validating until they either go in the field and tab out or type something in? I tried changing the dependencyTriggers to a few different things, but none seemed to work. I changed the dependencyTriggers to undefined and it got rid of the error, but also got rid of them when I want them there.

I also tried to manually remove the 'has-error' from my divs just on page load, but that was unsuccessful as well.

public bool AddressOutsideUS { get; set; }

[RequiredIf("AddressOutsideUS == false")]
public string MailingCity { get; set; }

image

Just to clarify, the validations are working. When I check the box, the fields are no longer required and the errors go away. When I uncheck it, they become required and have red around them. What I would like is to only turn red when you actually type or lose focus inside the box.

Edit: I am also looking to see if there's an easy way to figure out what is triggering the change as well.

jwaliszko commented 9 years ago

Hi,

try to use empty string to initialize triggers, i.e. ea.settings.dependencyTriggers = '';. There is a bug related to undefined reported and solved (but not yet released) here: https://github.com/JaroslawWaliszko/ExpressiveAnnotations/issues/64. Also you can provide desired events names on which you'd like validate dependent fields (custom events are also possible, e.g. see afterpaste event shown in web sample project: https://github.com/JaroslawWaliszko/ExpressiveAnnotations/tree/master/src/ExpressiveAnnotations.MvcWebSample).

Regards

speshulk926 commented 9 years ago

I will try this. I think the specific issue is with another addon that displays the error messages on the validation summary. The addon also formats the errors to work with bootstrap. When the fields are shown, it is running all of the validations against it which include required.

I don't think the issue I'm facing is related to RequiredIf. I'll keep playing with it and see what I can come up with.

Thanks for your response.

speshulk926 commented 9 years ago

That worked for me when I set dependencyTriggers = ''. I thought when I did that it was going to remove the ability to auto-mark my fields as invalid while typing, pasting, etc, but it seems OK.

Thanks

jwaliszko commented 9 years ago

Good it works for you. Regards.