S
S
Sergey Beloventsev2018-08-07 16:13:53
Yii
Sergey Beloventsev, 2018-08-07 16:13:53

How to work with MultipleLoad and MultipleValidate correctly?

Here is the model

class PhoneForm extends Model
{
    public $value;
    public $type;
    public function rules()
    {
        return [
            ['value', 'required'],
            ['type', 'integer'],
            ['value', 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'type' => 'Тип',
            'value' => 'Значение',
        ];
    }
}
This is how it is implemented in the form
<?php foreach ($model->phones as $key => $phone): ?>
                <div class="row">
                    <div class="col-md-6">
                        <?= $form->field($phone, "[$key]type")->dropDownList($phone->typeArr)->label(false) ?>
                    </div>
                    <div class="col-md-6">
                        <?= $form->field($phone, "[$key]value")->textInput(['maxlength' => true, 'placeholder' => 'Номер'])->label(false) ?>
                    </div>
                </div>
            <?php endforeach ?>

The model is not ActiveRecord and is not inherited from it (so just in case) Tell me this is how it will be right to do multipleValidation
class PhoneService
{
    public $form;
    public $user_id;
    public function __construct(int $id)
    {
        $this->form=new PhoneForm();
        $this->user_id=$id;
    }
    public function create(array $phones){
        if(Model::loadMultiple($this->form,$phones)&& Model::validateMultiple($this->form)){
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question