Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组字段中的Yii2必需的验证器_Yii2_Yii2 Validation - Fatal编程技术网

数组字段中的Yii2必需的验证器

数组字段中的Yii2必需的验证器,yii2,yii2-validation,Yii2,Yii2 Validation,在我的代码中,我有一个字段(例如字段名为exampleField[]),我在其中通过JS实现了添加更多的功能。现在,我想在每个字段上实现Yii2必需的Validtor。我已经使用了每个验证器,但它不起作用。我的yii2版本是2.0.6。提前谢谢 这是我的javascript代码 var max_fields = 4; //maximum input boxes allowed var wrapper = $(".addAmount"); //Fields wrapper

在我的代码中,我有一个字段(例如字段名为exampleField[]),我在其中通过JS实现了添加更多的功能。现在,我想在每个字段上实现Yii2必需的Validtor。我已经使用了每个验证器,但它不起作用。我的yii2版本是2.0.6。提前谢谢

这是我的javascript代码

var max_fields = 4; //maximum input boxes allowed
        var wrapper = $(".addAmount"); //Fields wrapper
        var add_button = $(".add_field_button"); //Add button ID
        var html = '';

        var x = <?php echo $count ?>; //initlal text box count

        $(add_button).click(function (e) { //on add input button click

            html = '<div id="multipleRow' + x + '"><div class="fullwidth">';
            html += '<div class="span4"><div class="control-group">';
            html += '<label for="plandetail-planduration" class="control-label">Duration <span class="required">*</span></label>';
            html += '<div class="controls"><div class="form-group field-plandetail-planduration required">';
            html += '<input type="text" name="PlanDetail[planDuration][]" class="form-control" id="plandetail-planduration">';
            html += '<div class="error"></div></div></div></div>';
            html += '</div>';
            html += '<div class="span4"><div class="control-group">';
            html += '<label for="plandetail-plandurationtype" class="control-label">Duration Period<span class="required">*</span></label>';
            html += '<div class="controls"><div class="form-group field-plandetail-plandurationtype">';
            html += '<select name="PlanDetail[planDurationType][]" class="form-control" id="plandetail-plandurationtype">';
            html += '<option value=""></option>';
            html += '<option value="month">Month</option>';
            html += '<option value="year">Year</option>';
            html += '</select>';
            html += '<div class="help-block"></div>';
            html += '</div></div></div></div></div>';
            html += '<div class="fullwidth">';
            html += '<div class="span4"><div class="control-group">';
            html += '<label for="plandetail-planamount" class="control-label">Amount <span class="required">*</span></label>';
            html += '<div class="controls"><div class="form-group field-plandetail-planamount required">';
            html += '<input type="text" name="PlanDetail[planAmount][]" class="form-control" id="plandetail-planamount">';
            html += '<div class="error"></div>';
            html += '</div></div>';
            html += '</div></div>';
            html += '<div class="span4"><div class="control-group">';
            html += '<label for="plandetail-plandiscountamount" class="control-label">Discount Amount <span class="required">*</span></label>';
            html += '<div class="controls"><div class="form-group field-plandetail-plandiscountamount required">';
            html += '<input type="text" name="PlanDetail[planDiscountAmount][]" class="form-control" id="plandetail-plandiscountamount">';
            html += '<div class="error"></div>';
            html += '</div></div>';
            html += '</div></div>';
            html += '<div class="span4"><div class="control-group">';
            html += '<label for="plandetail-plancurrency" class="control-label">Currency <span class="required">*</span></label>';
            html += '<div class="controls"><div class="form-group field-plandetail-plancurrency required">';
            html += '<select name="PlanDetail[planCurrency][]" class="form-control" id="plandetail-plancurrency">';
            html += '<option value=""></option><option value="USD">USD</option><option value="EUR">EUR</option>';
            html += '</select>';
            html += '<div class="help-block"></div>';
            html += '</div></div>';
            html += '</div></div>';
            html += '</div>';
            html += '<div class="col-lg-4"><a href="javascript:void(0)" class="remove_field" id="' + x + '">Remove</a></div>';
            html += '</div></div>';
            e.preventDefault();
            if (x < max_fields) { //max input box allowed
                x++; //text box increment
                $(wrapper).append(html); //add input box
            }else{alert('You Can not add more than 4 Amount')}

        });
        $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
            e.preventDefault();
//            alert(this.id);
            $("#multipleRow" + this.id).remove();
            x--;
        });
var max_字段=4//允许的最大输入框数
变量包装=$(“.addAmount”)//字段包装器
var add_button=$(“.add_field_button”)//添加按钮ID
var html='';
var x=//初始文本框计数
$(添加按钮)。单击(函数(e){//在添加输入按钮上单击
html='';
html+='';
html+=“持续时间*”;
html+='';
html+='';
html+='';
html+='';
html+='';
html+=“持续时间*”;
html+='';
html+='';
html+='';
html+=“月”;
html+=‘年’;
html+='';
html+='';
html+='';
html+='';
html+='';
html+=“金额*”;
html+='';
html+='';
html+='';
html+='';
html+='';
html+='';
html+=“折扣金额*”;
html+='';
html+='';
html+='';
html+='';
html+='';
html+='';
html+=“货币*”;
html+='';
html+='';
html+=‘USDEUR’;
html+='';
html+='';
html+='';
html+='';
html+='';
html+='';
html+='';
e、 预防默认值();
如果(x
您只添加输入的
html
,而缺少进行验证的
js
。必须使用添加所有动态字段。下面是一个带有基本模板的联系人表单示例:

首先,从表单中删除字段email,这样我们就可以使用
js

<?php // $form->field($model, 'email') ?>

每个验证员都应该工作。我猜生成这些字段的javascript可能有问题。用相关代码更新您的问题,以便人们能够提供更多帮助。到目前为止,我们只能猜测发生了什么。请把你的codes@Clyff我更新了我的代码
<?php $form = ActiveForm::begin(['id' => 'contact-form', 'enableAjaxValidation' => true]); ?>
<?php
$js = <<<JS
$( document ).ready(function() {
    var input = '<div class="form-group field-contactform-email required">' +
        '<label class="control-label" for="contactform-email">Email</label>' +
        '<input type="text" id="contactform-email" class="form-control" name="ContactForm[email]">' +
        '<p class="help-block help-block-error"></p>' +
    '</div>';

    $('#contact-form').append(input); // add the html in the form

    $('#contact-form').yiiActiveForm('add', { // add the js rules to the form
        'id': 'contactform-email',
        'name': 'email',
        'container': '.field-contactform-email',
        'input': '#contactform-email',
        'enableAjaxValidation':true
    });
});
JS;

$this->registerJs($js); // insert javascript
use yii\web\Response;
use yii\widgets\ActiveForm;

class SiteController extends Controller
{
    ...

    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post())) { // load the post
            if (Yii::$app->request->isAjax) { // verify is ajax
                Yii::$app->response->format = Response::FORMAT_JSON; // change the response to json format
                return ActiveForm::validate($model); // return the validation as json
            } elseif ($model->contact(Yii::$app->params['adminEmail'])) {
                Yii::$app->session->setFlash('contactFormSubmitted');

                return $this->refresh();
            }
        }

        return $this->render('contact', [
            'model' => $model,
        ]);
    }
}