Yii2验证动态字段

时间:2017-05-05 13:04:07

标签: javascript php yii2

我有Yii2 ActiveForm,并且字段名称和位置必须是动态字段,因此用户可以添加多个名称和位置。

<?php $form = ActiveForm::begin([
        'id'=>'form-id',
        'enableClientValidation' => true,
]); ?>

<?= $form->field($model, 'name')->textInput() ?>
<?= $form->field($model, 'location')->textInput() ?>

    <h5><strong>Additional info</strong></h5>
    <div class="table-responsive">
        <table id="myTable" class="table table-hover info-list">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Location</th>
                    <th></th>
                </tr>           
            </thead>

        </table>
        <div class="form-group">
            <button type="button" class="btn btn-default" id="addRow"><i class="glyphicon glyphicon-plus-sign"></i> Add Year</button>
        </div>
    </div>

    <div class="form-group text-right">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('main', 'Create') : Yii::t('main', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

<?php ActiveForm::end(); ?>

我使用此javascript代码添加/删除这些输入,并尝试通过yiiActiveForm在动态字段上添加验证(&#34;添加&#34;);

var counter = 0;

$(document).ready(function () {

$("#addRow").click(function () {
    counter = $('#myTable tr').length - 1;

    var newRow = $("<tr>");
    var cols = "";

    cols += "<td><div class='form-group field-model-" + counter + "-period required'>" +
        "<input type='text' id='model-" + counter + "-name' class='form-control' name='Model[" + counter + "][name]'>" +
        "<div class='help-block' id='model_error_" + counter + "_period'></div></div></td>";

    cols += "<td><div class='form-group field-model-" + counter + "-year required'>" +
        "<input type='text' id='model-" + counter + "-year' class='form-control' name='Model[" + counter + "][year]'>" +
        "<div class='help-block' id='Model_error_" + counter + "_year'></div></div></td>";

    cols += '<td><a class="btn btn-xs btn-danger" href="#" onclick="deleteRow(this);return false;"><span class="glyphicon glyphicon-trash"></span></a></td>';


    newRow.append(cols);
    if (counter == 2) $('#addRow').attr('disabled', true);
    $("table.info-list").append(newRow);
    counter++;

     $("#form-id").yiiActiveForm("add",{
         'id': 'model-' + counter + '-name', 
         'name': 'Model["' + counter + '"][name]',
         'container': '.field-model-' + counter + '-name',
         'input': 'model-' + counter + '-name',
         "error": ".help-block.help-block-error",
         "validate": function(attribute, value, messages, deferred, $form) {

             yii.validation.required(value, messages, {
                 "message": "Name be blank blank."
             });
         }
     });
     $("#form-id").yiiActiveForm("add",{
         'id': 'model-' + counter + '-location', 
         'name': 'Model["' + counter + '"][location]',
         'container': '.field-model-' + counter + '-location',
         'input': 'model-' + counter + '-location',
         "error": ".help-block.help-block-error",
         "validate": function(attribute, value, messages, deferred, $form) {

             yii.validation.required(value, messages, {
                 "message": "Location be blank blank."
             });
         }
     });
});
});

验证仅适用于PHP代码生成的标准第一个输入,但不适用于使用Javascript创建的标准第一个输入。如何在Yii2中验证动态字段?

0 个答案:

没有答案