@@ -92,6 +92,10 @@ public function init()
9292 throw new InvalidConfigException ("The 'name' option is required. " );
9393 }
9494
95+ if ($ this ->enableError && empty ($ this ->widget ->model )) {
96+ throw new InvalidConfigException ('Property "enableError" available only when model is defined. ' );
97+ }
98+
9599 if (is_null ($ this ->type )) {
96100 $ this ->type = self ::TYPE_TEXT_INPUT ;
97101 }
@@ -207,16 +211,38 @@ public function renderCellContent($value, $index)
207211 return $ input ;
208212 }
209213
214+ $ hasError = false ;
210215 if ($ this ->enableError ) {
211- $ input .= "\n" . Html::tag ('div ' , '' , $ this ->errorOptions );
216+ $ attribute = $ this ->widget ->attribute . $ this ->widget ->getElementName ($ this ->name , $ index , false );
217+ $ error = $ this ->widget ->model ->getFirstError ($ attribute );
218+ $ hasError = !empty ($ error );
219+ $ input .= "\n" . $ this ->renderError ($ error );
212220 }
213221
214- $ input = Html::tag ('div ' , $ input , [
215- 'class ' => 'form-group field- ' . $ options ['id ' ],
216- ]);
222+ $ wrapperOptions = [
223+ 'class ' => 'form-group field- ' . $ options ['id ' ]
224+ ];
225+
226+ if ($ hasError ) {
227+ Html::addCssClass ($ wrapperOptions , 'has-error ' );
228+ }
229+ $ input = Html::tag ('div ' , $ input , $ wrapperOptions );
217230
218231 return Html::tag ('td ' , $ input , [
219232 'class ' => 'list-cell__ ' . $ this ->name ,
220233 ]);
221234 }
235+
236+ /**
237+ * @param string $error
238+ * @return string
239+ */
240+ private function renderError ($ error )
241+ {
242+ $ options = $ this ->errorOptions ;
243+ $ tag = isset ($ options ['tag ' ]) ? $ options ['tag ' ] : 'div ' ;
244+ $ encode = !isset ($ options ['encode ' ]) || $ options ['encode ' ] !== false ;
245+ unset($ options ['tag ' ], $ options ['encode ' ]);
246+ return Html::tag ($ tag , $ encode ? Html::encode ($ error ) : $ error , $ options );
247+ }
222248}
0 commit comments