Skip to content

Commit

Permalink
Fix display inline errors in case using form without ajax validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Tupikov committed Jul 26, 2015
1 parent 4bed574 commit 8ac7f9b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/multiple_columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ use yii\helpers\Html;
[
'name' => 'priority',
'title' => 'Priority',
'enableError' => true,
'options' => [
'class' => 'input-priority'
]
Expand Down
8 changes: 6 additions & 2 deletions examples/actions/MultipleInputAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public function run()
return $result;
}

if ($model->load(Yii::$app->request->post()) && $model->validate()) {

if ($model->load(Yii::$app->request->post())) {
if (!$model->validate()) {
Yii::error('Validation errors: ' . print_r($model->getErrors(), true));
}
}


return $this->controller->render('@unclead-examples/views/example.php', ['model' => $model]);
}
}
16 changes: 10 additions & 6 deletions examples/models/ExampleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function init()
$this->schedule = [
[
'day' => '27.02.2015',
'user_id' => 1,
'user_id' => 31,
'priority' => 1
],
[
'day' => '27.02.2015',
'user_id' => 2,
'user_id' => 33,
'priority' => 2
],
];
Expand Down Expand Up @@ -139,10 +139,14 @@ public function validateSchedule($attribute)

foreach($this->$attribute as $index => $row) {
$error = null;
$requiredValidator->validate($row['priority'], $error);
if (!empty($error)) {
$key = $attribute . '[' . $index . '][priority]';
$this->addError($key, $error);
foreach (['user_id', 'priority'] as $name) {
$error = null;
$value = isset($row[$name]) ? $row[$name] : null;
$requiredValidator->validate($value, $error);
if (!empty($error)) {
$key = $attribute . '[' . $index . '][' . $name . ']';
$this->addError($key, $error);
}
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/MultipleInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ private function renderRowContent($index = null, $data = null)
private function collectJsTemplates()
{
if (is_array($this->getView()->js) && array_key_exists(View::POS_READY, $this->getView()->js)) {
$this->jsTemplates = [];
foreach ($this->getView()->js[View::POS_READY] as $key => $js) {
$this->jsTemplates = [];
foreach ($this->getView()->js[View::POS_READY] as $key => $js) {
if (preg_match('/^.*' . $this->options['id'] . '-{multiple-index}.*$/', $js) === 1) {
$this->jsTemplates[] = $js;
unset($this->getView()->js[View::POS_READY][$key]);
Expand Down Expand Up @@ -301,17 +301,20 @@ private function renderActionColumn($index = null)
* Returns element's name.
*
* @param string $name the name of cell element
* @param int|null $index
* @param int|null $index current row index
* @param bool $withPrefix whether to add prefix.
* @return string
*/
public function getElementName($name, $index)
public function getElementName($name, $index, $withPrefix = true)
{
if (is_null($index)) {
$index = '{multiple-index}';
}
return $this->getInputNamePrefix($name) . (
count($this->columns) > 1 ? '[' . $index . '][' . $name . ']' : '[' . $name . '][' . $index . ']'
);
$elementName = count($this->columns) > 1
? '[' . $index . '][' . $name . ']'
: '[' . $name . '][' . $index . ']';
$prefix = $withPrefix ? $this->getInputNamePrefix($name) : '';
return $prefix . $elementName;
}

/**
Expand Down
34 changes: 30 additions & 4 deletions src/MultipleInputColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function init()
throw new InvalidConfigException("The 'name' option is required.");
}

if ($this->enableError && empty($this->widget->model)) {
throw new InvalidConfigException('Property "enableError" available only when model is defined.');
}

if (is_null($this->type)) {
$this->type = self::TYPE_TEXT_INPUT;
}
Expand Down Expand Up @@ -207,16 +211,38 @@ public function renderCellContent($value, $index)
return $input;
}

$hasError = false;
if ($this->enableError) {
$input .= "\n" . Html::tag('div', '', $this->errorOptions);
$attribute = $this->widget->attribute . $this->widget->getElementName($this->name, $index, false);
$error = $this->widget->model->getFirstError($attribute);
$hasError = !empty($error);
$input .= "\n" . $this->renderError($error);
}

$input = Html::tag('div', $input, [
'class' => 'form-group field-' . $options['id'],
]);
$wrapperOptions = [
'class' => 'form-group field-' . $options['id']
];

if ($hasError) {
Html::addCssClass($wrapperOptions, 'has-error');
}
$input = Html::tag('div', $input, $wrapperOptions);

return Html::tag('td', $input, [
'class' => 'list-cell__' . $this->name,
]);
}

/**
* @param string $error
* @return string
*/
private function renderError($error)
{
$options = $this->errorOptions;
$tag = isset($options['tag']) ? $options['tag'] : 'div';
$encode = !isset($options['encode']) || $options['encode'] !== false;
unset($options['tag'], $options['encode']);
return Html::tag($tag, $encode ? Html::encode($error) : $error, $options);
}
}

0 comments on commit 8ac7f9b

Please sign in to comment.