Skip to content

Commit 9a78a5a

Browse files
committedJul 28, 2023
refactor: default to using plain attribute name when adding model result to result object
BREAKING CHANGE: add option forActiveForm=true to existing calls to addModelResult()
1 parent e938ba0 commit 9a78a5a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed
 

‎FormHelper.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,24 @@ public static function processAjaxSubmitError($options = []) {
109109
*
110110
* @param winternet\yii2\Result|null : Result instance, or provide null to have the method create an instance for you
111111
* @param yii\base\model $model : A Yii model
112+
* @param array $options : Available options:
113+
* - `forActiveForm` : set true to use attribute names as needed by ActiveForm
112114
*
113115
* @return winternet\yii2\Result
114116
*/
115-
public static function addModelResult($result, &$model) {
117+
public static function addModelResult($result, &$model, $options = []) {
116118
if (!$result) {
117119
$result = new \winternet\yii2\Result();
118120
}
119121

120122
// Add errors
121123
foreach ($model->getErrors() as $attribute => $errors) {
122-
// Generate the form field ID so Yii ActiveForm client-side can apply the error message
123-
$attributeId = \yii\helpers\Html::getInputId($model, $attribute);
124+
if (!empty($options['forActiveForm'])) {
125+
// Generate the form field ID so Yii ActiveForm client-side can apply the error message
126+
$attributeId = \yii\helpers\Html::getInputId($model, $attribute);
127+
} else {
128+
$attributeId = $attribute;
129+
}
124130

125131
$result->addNamedErrors($attributeId, $errors);
126132
}

0 commit comments

Comments
 (0)
Please sign in to comment.