Yii2: ActiveField: show an error for element array

Created on 29 Feb 2016  ·  2Comments  ·  Source: yiisoft/yii2

I have a form:

...
<div class="row">
    <div class="col-md-4 col-md-4 col-sm-4 col-xs-6 col-lg-4"><?= $form->field($formModel, 'meta_title['.$lang['code'].']')->textInput(['maxlength' => true]) ?></div>
    <div class="col-md-4 col-md-4 col-sm-4 col-xs-6 col-lg-4"><?= $form->field($formModel, 'meta_description['.$lang['code'].']')->textInput(['maxlength' => true]) ?></div>
    <div class="col-md-4 col-md-4 col-sm-4 col-xs-6 col-lg-4"><?= $form->field($formModel, 'meta_keywords['.$lang['code'].']')->textInput(['maxlength' => true]) ?></div>
</div>
...

For each language in the system I generate a new field.
Next, in my model (extends yii\base\Model) I want to do validation data. For example like this:

$this->addError('title[en]', 'Ошибка только для поля с языком en');

But the error is not show during render a my form. This code:
https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php#L2033
always cuts [*].

So there's a possibility validate field title only globally. That is all fields title[*].
Why is not possible to validate only a specific element of the array (example title[en]) ?


На случай, если никто не поймет моего топорного английского:

У меня есть форма. (код выше)
Для каждого языка в системе я генерирую новое поле. Далее в моей моделе (extends yii\base\Model), я хочу сделать валидацию данных. Например вот так:

$this->addError('title[en]', 'Ошибка только для поля с языком en');

Но при рендере формы ошибка не показывается. Вот этот код:
https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php#L2033
всегда вырезает [*].

Исходя из этого есть возможность валидировать поле title только глобально, тоесть для всех подключенных полей title[*]. Почему нет возможности валидировать только конкретный элемент массива, например title[en] ?

Most helpful comment

Why validation of the array attribute element is not supported? It is very interesting to me.

All 2 comments

Validation of the array attribute element is not suported.
You should use tabular input, which means usage of separated model per each language specific input set.

You may investigate following extension for the inspiration:
https://github.com/yii2tech/ar-variation

Why validation of the array attribute element is not supported? It is very interesting to me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gpoehl picture gpoehl  ·  47Comments

Mirocow picture Mirocow  ·  56Comments

sepidemahmoodi picture sepidemahmoodi  ·  104Comments

schmunk42 picture schmunk42  ·  47Comments

sapsxxxil picture sapsxxxil  ·  50Comments