How To: Custom Client Validation On Checkbox Toggle and Optional Input Field

Comparing #1 with #2

Revision #2 was created by Wade Shuler Wade Shuler on Jul 5, 2016, 9:28:17 AM.

fix heading tag

Content

[...]
You **do not** need to reset the status of all the inputs on your form.

You **do not** need to set the "submitting" value to true, as illustrated in the Yii2 Cookbook (linked to from the Yii2 docs on Client Validation).


##
# Use Case

You have a form with a checkbox and a text input. The checkbox is not required, but if it is checked, the text input should then be required. If the checkbox is un-checked, the text input should no longer be required. In my case, it is for using Gravatar for user profile pics. If they have Gravatar, they check the box and enter in their Gravatar email address (since it may not match the email they used to register on my site). Later, I will be pairing it with cebe's yii2-gravatar extension. It is actually quite simple :)

**Model:**
[...]