YII2 : Add Dynamic form fields and their validations

后端 未结 3 1147
鱼传尺愫
鱼传尺愫 2020-12-17 17:44

I am adding dynamic form fields onChange of dropdown. Both types of fields are coming from different models and go to the database in different tables. I have already define

3条回答
  •  攒了一身酷
    2020-12-17 18:34

    Simple

    You should try this

    registerJs('
    
    
    
                jQuery("#w0").yiiActiveForm("add",{
                    "id": "customer-name",
                    "name": "name",
                    "container": ".field-customer-name",
                    "input": "#customer-name",
                    "error": ".help-block.help-block-error",
                    "validate": function(attribute, value, messages, deferred, $form) {
    
                        yii.validation.required(value, messages, {
                            "message": "Name be blank bug."
                        });
    
                        yii.validation.string(value, messages, {
                            "message": "Name must be a string.",
                            "max": 255,
                            "tooLong": "Name should contain at most 255 characters.",
                            "skipOnEmpty": 1
                        });
                    }
            });
    
    
        ');
     ?>
    

    Changes

    • w0 into your form ID

    • "id": "customer-name" into your input field ID

    • "container": ".field-customer-name" into input field div container class

提交回复
热议问题