R
R
Roma Antonyuk2017-07-27 16:22:47
Yii
Roma Antonyuk, 2017-07-27 16:22:47

ActiveField validation via ajaxValidation ActiveForm?

There are fields that are loaded via ajax into the created form.

$('#add-child').click(function (e) {
        e.preventDefault();
        var number = $('.tourists .tourist').length,
            count_adults = $('.tourists .adult').length,
            count_children = $('.tourists .child').length;

        if(count_adults > count_children)
        {
            $.ajax({
                url: '/order/default/add-child',
                method: 'GET',
                data: { number: number },
                success: function (data) {
                    $('.tourists').append(data);
                    $('.selectpicker').selectpicker();
                    $('[data-toggle="tooltip"]').tooltip();
                    if(count_children + 1 >= count_adults) $('#add-child-block').hide();
                }
            });
        }
    });

Controller code:
public function actionAddChild($number)
    {
        return $this->renderPartial('_add-child', [
            'tourist' => new Tourist(),
            'number' => $number
        ]);
    }

View code to be loaded:
<?php
use yii\helpers\Html;
use kartik\icons\Icon;
use yii\widgets\ActiveField;
/* @var $tourist \common\models\Tourist */

?>
<?php
$sex = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']sex']);
$surname = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']surname']);
$name = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']name']);
$date_of_birth_text = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']date_of_birth_text']);
$passport_series = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']passport_series']);
$passport_number = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']passport_number']);
$passport_valid_until_text = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']passport_valid_until_text']);
$visa = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']visa']);
$child_documents = new ActiveField(['form' => 'w2', 'model' => $tourist, 'attribute' => '['. $number .']child_documents']);
?>
<div class="col-md-6">
    <div class="row small-gutters">
        <div class="col-md-5">
            <div class="form-group">
                <?= Html::label('Пол', '', ['class' => 'form-label']) ?>

                <?= $sex->dropDownList([
                    '1' => 'Мужской',
                    '2' => 'Женский'
                ], ['class' => 'form-control selectpicker'])->label(false) ?>
            </div>
        </div>
        <div class="col-md-7">
            <div class="form-group">
                <?= Html::label('Фамилия', '', ['class' => 'form-label']) ?>
                <?= $surname->textInput()->label(false) ?>
            </div>
        </div>
    </div>
</div>
<div class="col-md-6">
    <div class="row small-gutters">
        <div class="col-md-3">
            <div class="form-group">
                <?= Html::label('Имя', '', ['class' => 'form-label']) ?>

                <?= $name->textInput()->label(false) ?>
            </div>
        </div>
        <div class="col-md-5">
            <div class="form-group">
                <?= Html::label('Дата рождения', '', ['class' => 'form-label']) ?>

                <?= Html::a($date_of_birth_text->textInput(['placeholder' => 'дд.мм.гг'])->label(false), '#tourist-date-birth-'. $number, ['class' => 'date-single open-popup']) ?>
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group">
                <?= Html::label('Вписан в паспорт', '', ['class' => 'form-label']) ?>

                <?= $child_documents->dropDownList([
                    1 => 'Да',
                    0 => 'Нет'
                ], ['class' => 'form-control selectpicker'])->label(false) ?>
            </div>
        </div>
    </div>
</div>

<div class="col-md-8">
    <div class="row small-gutters">
        <div class="col-md-8">
            <div class="form-group">
                <?= Html::label('Паспорт  (серия, номер)', '', ['class' => 'form-label']) ?>

                <div class="row small-gutters">
                    <div class="col col-sm-4">
                        <?= $passport_series->textInput(['placeholder' => 'AA'])->label(false) ?>
                    </div>
                    <div class="col col-sm-8">
                        <?= $passport_number->textInput(['placeholder' => '1234567890123'])->label(false) ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group">
                <?= Html::label('Действителен до'. Html::a(Icon::show('info-circle'), '#', [
                        'class' => 'tooltip-link addition-info',
                        'data-toggle' => 'tooltip',
                        'data-placement' => 'bottom',
                        'data-html' => 'true',
                        'data-animation' => 'false',
                        'title' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa, perspiciatis?'
                    ]), '', [
                    'class' => 'form-label'
                ]) ?>

                <?= Html::a($passport_valid_until_text->textInput(['placeholder' => 'дд.мм.гг'])->label(false),
                    '#tourist-passport-valid-until-'. $number, ['class' => 'date-single open-popup']) ?>
            </div>
        </div>
    </div>
</div>
<div class="col-md-4">
    <div class="form-group">
        <?= Html::label('Виза' . Html::a(Icon::show('info-circle'), '#', [
                'class' => 'tooltip-link addition-info',
                'data-toggle' => 'tooltip',
                'data-placement' => 'bottom',
                'data-html' => 'true',
                'data-animation' => 'false',
                'title' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa, perspiciatis?'
            ]), '', [
            'class' => 'form-label'
        ]) ?>

        <?= $visa->dropDownList([
            0 => 'Ненужна',
            1 => 'Нужна'
        ], [
            'class' => 'form-control selectpicker'
        ])->label(false) ?>
    </div>
</div>

Validation:
$client = new Client(['scenario' => 'hand_mode']);
        $passport = new Passport();
        $order = new Order(['scenario' => 'hand_mode']);
        $tourists = \common\models\Model::createMultiple(Tourist::className());

        if(Yii::$app->request->isAjax && $client->load(Yii::$app->request->post()) && $passport->load(Yii::$app->request->post()) && $order->load(Yii::$app->request->post()) && Model::loadMultiple($tourists, Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;

            $validate = [];
            $validate = array_merge(ActiveForm::validate($client), $validate);
            $validate = array_merge(ActiveForm::validate($order), $validate);
            $validate = array_merge(ActiveForm::validate($passport), $validate);
            $validate = array_merge(ActiveForm::validateMultiple($tourists), $validate);
            return $validate;
        }

The problem is that the validation of these fields passes and errors are returned, but they are not displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2017-07-27
@karminski

So you form do, as it is told in a manual. You have made your life so difficult that you want to cry.
www.yiiframework.com/doc-2.0/guide-input-forms.htm...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question