1

Multiple inputs with same name are added in a form dynamically using jQuery. So now how can I validate these dynamic inputs using yii2 validation rules at client side ?

AsgarAli
  • 2,201
  • 1
  • 20
  • 32
  • I have used each validator of Yii2 that will validate all input which have same name but the problem is that it does not validate at client side.It validates after form submission.If one input has error then it shows error under all inputs – AsgarAli Jun 11 '15 at 16:02
  • 1
    Update the question with the code you have and what you just commented so people have all the info they need. – Blizz Jun 11 '15 at 16:03
  • Please do you have any idea about this dynamic inputs ?Yii2 can't add validation code for dynamic inputs as these are not available at time of form initialization – AsgarAli Jun 11 '15 at 16:07

1 Answers1

5

After adding dynamic input using jQuery use below code to validate

jQuery('#form-id').yiiActiveForm("add", {
        "id":        "input-id",
        "name":      "input-name",
        "container": "#container-id or unique .container-class of this input",
        "input":     "#input-id or unique .input-class",
        "validate":  function (attribute, value, messages, deferred, $form) {
            yii.validation.required(value, messages, {"message": "Validation Message Here"});
        }
    }
);
robsch
  • 9,358
  • 9
  • 63
  • 104
AsgarAli
  • 2,201
  • 1
  • 20
  • 32