Is there an easy way with parsleyjs to make a field required depending on another field?
See my js fiddle here http://jsfiddle.net/marksteggles/wbhLq0t4/1/
<form data-parsley-validate="true">
<div class="form-group">
<label>
<input name="request_signature" type="checkbox" />Require signature</label>
<div class="request_signature_fields">
<textarea class="form-control required" name="signature_reason" rows="3"></textarea>
</div>
</div>
<input class="btn btn-success" name="commit" type="submit" value="Send" />
</form>
This should be possible with the great little Parsley addon plugin found here: http://themonk.github.io/parsely-conditions/
There is no easy way yet (see this and this).
You can either toggle the attribute
required
with Javascript, or listen to the right parsley events on one field and check the other field.Just incase anyone else is trying to work this out. The best way does seem to be altering the required attribute then clearing the values.
I used this:
HTML:
jQuery:
You can also hide the parts that are not required anymore, or disable the fields that are not needed, and add
[disabled]
and:hidden
to theexcluded
Parsley option.NB: you don't need to hide each field, hiding a parent
div
is enough.I found a good example that I forked here ➡️ http://jsfiddle.net/capripot/xoaLs4bt/
I realise that this question was asked and answered in 2012, and is most likely related to the ParsleyJS v1, while the most recent version at the time of writing this is v2.2.0. However I had to do some work on an old form that used v1 and I found that conditionals are possible (with a little bit of jQuery). So here's to anyone who might still need this.
You can dynamically add and remove form elements and constraints (read: validation rules) using the following:
So using jQuery listeneners for when the checkbox changes we can execute this kind of code which will add the signature field as a required field. Here it is in action for the question.
Given that everyone and their mother's are using front-end JS frameworks these days, I figured I'd toss in the approach I normally use. In my case (knockout js), I'll use the framework to add/remove those certain fields based on the value bound to the checkbox.