1
if($("#dataform").parsley().isValid()){
    $.ajax({
        url  : '<?php echo base_url();?>index.php/company/final_review?id=<?php echo $this->input->get('id'); ?>',
        type : 'POST',
        data : $("#dataform").serialize(),
        dataType:"json",
        beforeSend: function(){
            $('#remarks').parsley( 'addConstraint', { minlength: 5 });
        },
        success : function(data){
            console.log(data);
        }     
    });
}

This is my ajax part. The plugin is not validating the field it's submitting the form. The form validation is working fine. But i want to validate the remark field with min length . That is not working.

Zinnhead
  • 45
  • 4
Raj Parekh
  • 94
  • 1
  • 12
  • What version of Parsley are you using? Parsley 2.0 doesn't handle 'addConstraint'. Also, why add the constraint in the beforeSend event after you have already checked isValid()? – theduck Jun 22 '15 at 10:57
  • @theduck I am using Parsley 2.2 and it supports 'addConstraint'. where else can i add the constraint ? – Raj Parekh Jun 22 '15 at 11:20
  • 1
    I guess anywhere that is called before you check isValid(). – theduck Jun 22 '15 at 11:30
  • This is the reference to addConstraint being deprecated. http://stackoverflow.com/questions/21867162/how-can-i-dynamically-add-and-remove-form-fields-to-be-validated-by-parsley-js see the accepted answer. It may not be correct of course. – theduck Jun 22 '15 at 11:33
  • Or can you add `data-parsley-minlength` attribute to the field? – theduck Jun 22 '15 at 11:38
  • Thankyou @theduck now its working fine. – Raj Parekh Jun 22 '15 at 12:02
  • Did you move the addConstraint line or add the attribute in the end? Will post answer with correct fix so that others can benefit from it. – theduck Jun 22 '15 at 12:04

1 Answers1

1

Make sure this line:

$('#remarks').parsley( 'addConstraint', { minlength: 5 });

is called before you check isValid().

theduck
  • 2,589
  • 13
  • 17
  • 23