N
N
Nidxegg2016-07-15 08:26:46
Yii
Nidxegg, 2016-07-15 08:26:46

How to fix problem with validation of two models from one yii2 form?

Hello. There was a problem with the fact that when the validation is fully checked, an error occurs that the fields are not filled. Until it passes validation, hints appear as they should. When all fields are filled in correctly, the hints disappear and the data does not go. print validation errors says fields are not filled. It’s as if the post is disappearing somewhere, although inside the controller I can print out the data and it fills the fields with something.
controller code.

public function actionValueCounter()
    {
        $vladelcy = new Vladelcy();
        $dogovor = new Dogovor();


        if ($vladelcy->load(Yii::$app->request->post()) and $dogovor->load(Yii::$app->request->post()));   //при правильном заполнении код дальше не идет
        {
            $valid_dogovor = $dogovor->validate();
            $valid_vladelcy = $vladelcy->validate();
            
            if ($valid_vladelcy == TRUE && $valid_dogovor == TRUE) {
                $info_chel = Vladelcy::find()
                    ->joinWith('dogovor')
                    ->where(['dogovor.dogovorid' => $dogovor->dogovorid, 'vladelcy.familiya' => $vladelcy->familiya])
                    ->one();

                if (empty($info_chel)) {
                    echo 'совпадений не найдено';
                    die();
                } else {
                    $ulic = substr($info_chel->abonentid, 0, 4);
                    $ulica = Ulicy::findOne($ulic);

                    return $this->render('view_result_value', ['account' => $info_chel, 'ulica' => $ulica]);
                }
            }
        }
      return $this->render('valuecounter', ['vladelcy' => $vladelcy, 'dogovor' => $dogovor]);
    }

view code
<div class="x_content">
                <br />
                   <?php  $form = ActiveForm::begin([
                    'id' => 'demo-form2',
                       'action' => ['site/value-counter'],

                    'options' => [
                    'class' => 'form-horizontal form-label-left'],
                    ]);?>
                  < <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Лицевой счет<span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <?=$form->field($dogovor, 'dogovorid', ['inputOptions' => ['class' => 'form-control col-md-7 col-xs-12']])->textInput()->input('text', ['placeholder' => "номер вашего договора"])->label(false); ?>
                        </div>
                         </div>
                <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Фамилия владельца<span class="required"></span>
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                        <?=$form->field($vladelcy, 'familiya', ['inputOptions' => ['class' => 'form-control col-md-7 col-xs-12']])->textInput()->input('text', ['placeholder' => "Фамилия владельца"])->label(false); ?>
                    </div>
                </div>

                    <div class="ln_solid"></div>
                    <div class="form-group">
                        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">

                            <?= Html::submitButton('Выбрать', ['class' => 'btn btn-primary']) ?>
                        </div>
                    </div>

            <?php  $form = ActiveForm::end();?>
            </div>

model code
public static function tableName()
    {
        return 'dogovor';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['abonentid','dogovorid'], 'required'],
            ['dogovorid', 'integer'],
            [['abonentid', 'dogovordata', 'telefon', 'kategoriya', 'socialid', 'datas', 'datapo', 'deystvuyushiy', 'dataizmeneniya'], 'string'],
        ];
    }

public static function tableName()
    {
        return 'vladelcy';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['dogovorid', 'familiya'], 'required'],
            [['dogovorid', 'sobstvennostID', 'nomer'], 'integer'],
            [['abonentid'], 'string', 'max' => 15],
            [['familiya', 'imya', 'otchestvo'], 'string', 'max' => 200],
            [['datas'], 'string', 'max' => 50],
            [['dogovorid'], 'exist', 'skipOnError' => true, 'targetClass' => Dogovor::className(), 'targetAttribute' => ['dogovorid' => 'dogovorid']],
        ];
    }

public function getDogovor()
    {
        return $this->hasOne(Dogovor::className(), ['dogovorid' => 'dogovorid']);
    }

79328ff2d4fe405988ec23cbf8542702.jpg

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Fedorov, 2016-07-15
@Nidxegg

see the validation rules, for the dogovor model - you fill in the dogovorid attribute in the form, and the model itself defines the dogovorid and familiya attributes as MANDATORY to fill in. with the second model the situation is the same

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question