验证yii2

时间:2016-05-12 04:45:09

标签: select2 yii2

我在_form.php中有一个textInput。然后,我改变它成为select2。但在我更改后,验证无效。有什么问题?如何在select2中进行验证,使其不能为空? 这是代码:

_form.php这个

<?php

        $formatJs = <<< 'JS'
        var formatPenerima = function (penerima) {
            if (penerima.loading) {
                return penerima.text;
            }
            var markup =
            '<div class="row">' + 
                '<div class="col-sm-4">' +
                    '<b style="margin-left:5px">' + penerima.text + '</b>' + 
                '</div>' +
                '<div class="col-sm-3"><i class="fa fa-phone"></i> ' + penerima.telepon + '</div>' +
                '<div class="col-sm-4"><i class="fa fa-envelope"></i> ' + penerima.mail + '</div>' +
            '</div>';
            return '<div style="overflow:hidden;">' + markup + '</div>';
        };
        var formatPenerimaSelection = function (penerima) {
            return penerima.id || penerima.text;
        }
JS;

        // Register the formatting script
        $this->registerJs($formatJs, \yii\web\View::POS_HEAD);

        // Script to parse the results into the format expected by Select2
        $resultsJs = <<< JS
        function (data, params) {
            params.page = params.page || 1;
            return {
                results: data.results,    // check here
                /*pagination: {
                    more: (params.page * 30) < data.total_count
                }*/
            };
        }
JS;

        $url = Url::to(['/paket/jsonlist']);

        // Render your widget
        // Get data from dummy data
        echo $form->field($model, 'no_induk')->widget(Select2::className(), [
            'name' => 'kv-repo-template',
            'value' => '',
            'initValueText' => '',
            'options' => ['placeholder' => 'Cari pemilik ...', 'id' => 'pengambil'],
            'pluginOptions' => [
                'allowClear' => true,
                'minimumInputLength' => 1,
                'ajax' => [
                    'url' => $url,
                    'dataType' => 'json',
                    'delay' => 250,
                    'data' => new JsExpression('function(params) { return {q:params.term, page: params.page}; }'),
                    'processResults' => new JsExpression($resultsJs),
                    'cache' => true
                ],
                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                'templateResult' => new JsExpression('formatPenerima'),
                'templateSelection' => new JsExpression('formatPenerimaSelection'),
            ],
        ])->label('Pemilik');
    ?>

这是我的模型规则:

public function rules()
    {
        return [
            [['no_induk', 'nama', 'no_telepon', 'email', 'kategori_paket', 'nama_pengirim'/*, 'tanggal_sampai'*/],
                'required', 'message' => '{attribute} tidak boleh kosong.'],
            [['id_satpam_pengentry'], 'required', 'message' => 'Nama satpam tidak boleh kosong.'],
            [['kategori_paket', 'status', 'id_satpam_pengentry', 'id_satpam_penyetuju'], 'integer'],
            [['tanggal_sampai', 'tanggal_pengambilan'], 'safe'],
            [['no_induk', 'email'], 'string', 'max' => 255],
            [['nama', 'nama_pengirim', 'nama_pengambil'], 'string', 'max' => 128],
            [['no_telepon'], 'string', 'max' => 64]
        ];
    }

3 个答案:

答案 0 :(得分:0)

你必须把 class =&gt;您的选项中的“表单控制”并删除ID,因此验证将起作用

答案 1 :(得分:0)

echo $form->field($model, 'no_induk')->widget(Select2::className(), [
        'name' => 'kv-repo-template',
        'value' => '',
        'initValueText' => '',
        'options' => ['placeholder' => 'Cari pemilik ...', 'class' => 'form-control'],
        'pluginOptions' => [
            'allowClear' => true,
            'minimumInputLength' => 1,
            'ajax' => [
                'url' => $url,
                'dataType' => 'json',
                'delay' => 250,
                'data' => new JsExpression('function(params) { return {q:params.term, page: params.page}; }'),
                'processResults' => new JsExpression($resultsJs),
                'cache' => true
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateResult' => new JsExpression('formatPenerima'),
            'templateSelection' => new JsExpression('formatPenerimaSelection'),
        ],
    ])->label('Pemilik');

答案 2 :(得分:0)

您可以在“选项”数组中设置“ required = true”

'options' => ['placeholder' => 'Cari pemilik ...', 'class' => 'form-control', 'required' => true],