Hello there,
I’m facing a problem when I try to inform the user the result of the validation of multiple models using yii2-multiple-input.
I have a Policy with many Rules. On the Controller I pre-validate the rules and if they’re all ok, I save the policy and then its rules. But I find any error on the Rules, how I can inject them into the parent model the Rules in order yii2-multiple-input show it (e.g. $model->rules) ?
Controller:
public function actionCreate()
{
$model = new Policy();
if( $model->load(Yii::$app->request->post()) && $status= $model->validate() )
{
if( isset( Yii::$app->request->post()['Policy']['rules'] ) )
{
$objRules = array();
foreach( Yii::$app->request->post()['Policy']['rules'] as $index => $ruleAttributes )
{
$objRules[ $index ] = new Rule( $ruleAttributes );
$status&= $objRules[ $index ]->validate();
}
if( $status && $model->save() )
{
foreach( $objRules as $objRule )
{
$objRule->policy_id = $model->id;
$status&= $objRule->save();
}
}
}
if( $status )
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
View:
<?=
$form->field( $model, 'rules')->label(false)->widget(MultipleInput::className(), [
'max' => 16,
'columns' => [
[
'name' => 'name',
'title' => 'name',
'defaultValue' => Null,
],
[
'name' => 'direction',
'title' => 'direction',
'type' => MultipleInputColumn::TYPE_DROPDOWN,
'defaultValue' => 0,
'items' => [
0 => 'Ingress',
1 => 'Egress',
],
'headerOptions' => [
'style' => 'width: 150px;',
],
],
[...]
Thank you.