Question

I am most of the time stuck building web applications or SMBs. But I have my days working on front ends. One interesting JS library I recently met is Parsley.js for web form validity check. Really cool.

In a new project I am working, I decided to use Parsley, and am not sure how to do this.

I got a select box with two options, Yes, No. If Yes is selected, I need to show the user a text-box that is required.

I went through Parsley documentation nearly twice, but couldn't figure out a way. But I think this kind of forms are common and should be supported by Parsley.

Is it possible through Parsley.js? Would appreciate your advice.

Était-ce utile?

La solution

it can be done , just need to check your select box selected value, if is 'Yes' then add

$( '#field' ).parsley( 'addConstraint', { required: true } );

if the value is 'No'

$( '#field' ).parsley( 'removeConstraint', 'required' );

Autres conseils

If you are having trouble with this like I did, see what guillaumpotier posted. Destroy parsley, modify the inputs, then initialize parsley again; here is his code:

// destroy
$('#myForm').parsley('destroy');
// change attribute
$('#myTestField').data('minlength', 2);
// assign parsley to do the job
$('#myForm').parsley();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top