Client side validation messages not working for dynamically added fields

Hi there,

I’m adding fields dynamically to the form via jQuery, depending on what the user enters in other field. The problem is the error messages for each field are not being displayed, though the validation itself is working. This is the code snippet I use to add validation to the new fields, as per the Yii2 cookbok :

function addValidation(){

    $("#seriales").children().each(function (index, value){

        console.log("Added validation for serial " + index)

        $('#componente-hardware-form').yiiActiveForm("add", {

            "id":        "serial-" + index,

            "name":      "ComponenteHardware[serial-" + index + "]",

            "container": "#serial-" + index + "group",

            "input":     "#serial-" + index,

            "error":     "#serial-" + index + "group .help-block",

            "validate":  function (attribute, value, messages, deferred, \$form) {

                yii.validation.required(value, messages, {'message': 'Validation Message Here'});





Any clues?

Thanks in advance

For the elements dynamically created, you should use jQuery "on"

$("#container").on("click", ".detail", function (event) {

//your code here..


search for how to use "on" and adapt to your situation.

Sorry, not getting your point. As I said, the yii validation is already working with the code snippet from my first post. The only thing I’m missing is to display the error message under each input, as for the default active fields.