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 ?

3
  • 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 Commented Jun 11, 2015 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. Commented Jun 11, 2015 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 Commented Jun 11, 2015 at 16:07

1 Answer 1

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"});
        }
    }
);
Sign up to request clarification or add additional context in comments.

2 Comments

stackoverflow.com/questions/31286133/…, i am also stuck in the same situation
how you use the above code can you please elaborate this ali?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.