Commit 25c2f9bf by Alex-Code Committed by Carsten Brandt

Made error message encoding configurable for ActiveForm

Updated yii.activeform.js to use the encode option introduced in #4122. Updated ActiveForm with new option encodeErrorSummary fixes #4690, close #4691
parent f0996169
...@@ -181,6 +181,7 @@ Yii Framework 2 Change Log ...@@ -181,6 +181,7 @@ Yii Framework 2 Change Log
- Enh #4630: Added automatic generating of unique slug value to `yii\behaviors\Sluggable` (klimov-paul) - Enh #4630: Added automatic generating of unique slug value to `yii\behaviors\Sluggable` (klimov-paul)
- Enh #4644: Added `\yii\db\Schema::createColumnSchema()` to be able to customize column schema used (mcd-php) - Enh #4644: Added `\yii\db\Schema::createColumnSchema()` to be able to customize column schema used (mcd-php)
- Enh #4656: HtmlPurifier helper config can now be a closure to change the purifier config object after it was created (Alex-Code) - Enh #4656: HtmlPurifier helper config can now be a closure to change the purifier config object after it was created (Alex-Code)
- Enh #4691: Encoding on `ActiveForm` and `ActiveField` validation errors is now configurable (Alex-Code)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
}; };
var defaults = { var defaults = {
// whether to encode the error summary
encodeErrorSummary: true,
// the jQuery selector for the error summary // the jQuery selector for the error summary
errorSummary: undefined, errorSummary: undefined,
// whether to perform validation before submitting the form. // whether to perform validation before submitting the form.
...@@ -73,6 +75,8 @@ ...@@ -73,6 +75,8 @@
input: undefined, input: undefined,
// the jQuery selector of the error tag // the jQuery selector of the error tag
error: undefined, error: undefined,
// whether to encode the error
encodeError: true,
// whether to perform validation when a change is detected on the input // whether to perform validation when a change is detected on the input
validateOnChange: false, validateOnChange: false,
// whether to perform validation when the user is typing. // whether to perform validation when the user is typing.
...@@ -404,11 +408,15 @@ ...@@ -404,11 +408,15 @@
var $container = $form.find(attribute.container); var $container = $form.find(attribute.container);
var $error = $container.find(attribute.error); var $error = $container.find(attribute.error);
if (hasError) { if (hasError) {
$error.text(messages[attribute.id][0]); if (attribute.encodeError) {
$error.text(messages[attribute.id][0]);
} else {
$error.html(messages[attribute.id][0]);
}
$container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.successCssClass) $container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.successCssClass)
.addClass(data.settings.errorCssClass); .addClass(data.settings.errorCssClass);
} else { } else {
$error.text(''); $error.empty();
$container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.errorCssClass + ' ') $container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.errorCssClass + ' ')
.addClass(data.settings.successCssClass); .addClass(data.settings.successCssClass);
} }
...@@ -425,12 +433,18 @@ ...@@ -425,12 +433,18 @@
var updateSummary = function ($form, messages) { var updateSummary = function ($form, messages) {
var data = $form.data('yiiActiveForm'), var data = $form.data('yiiActiveForm'),
$summary = $form.find(data.settings.errorSummary), $summary = $form.find(data.settings.errorSummary),
$ul = $summary.find('ul').html(''); $ul = $summary.find('ul').empty();
if ($summary.length && messages) { if ($summary.length && messages) {
$.each(data.attributes, function () { $.each(data.attributes, function () {
if ($.isArray(messages[this.id]) && messages[this.id].length) { if ($.isArray(messages[this.id]) && messages[this.id].length) {
$ul.append($('<li/>').text(messages[this.id][0])); var error = $('<li/>');
if (data.settings.encodeErrorSummary) {
error.text(messages[this.id][0]);
} else {
error.html(messages[this.id][0]);
}
$ul.append(error);
} }
}); });
$summary.toggle($ul.find('li').length > 0); $summary.toggle($ul.find('li').length > 0);
......
...@@ -63,6 +63,7 @@ class ActiveField extends Component ...@@ -63,6 +63,7 @@ class ActiveField extends Component
* The following special options are recognized: * The following special options are recognized:
* *
* - tag: the tag name of the container element. Defaults to "div". * - tag: the tag name of the container element. Defaults to "div".
* - encode: whether to encode the error output. Defaults to true.
* *
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/ */
...@@ -726,6 +727,7 @@ class ActiveField extends Component ...@@ -726,6 +727,7 @@ class ActiveField extends Component
} else { } else {
$options['error'] = isset($this->errorOptions['tag']) ? $this->errorOptions['tag'] : 'span'; $options['error'] = isset($this->errorOptions['tag']) ? $this->errorOptions['tag'] : 'span';
} }
$options['encodeError'] = !isset($this->errorOptions['encode']) || $this->errorOptions['encode'] !== false;
return $options; return $options;
} else { } else {
......
...@@ -54,6 +54,10 @@ class ActiveForm extends Widget ...@@ -54,6 +54,10 @@ class ActiveForm extends Widget
*/ */
public $fieldConfig; public $fieldConfig;
/** /**
* @var boolean whether to perform encoding on the error summary.
*/
public $encodeErrorSummary = true;
/**
* @var string the default CSS class for the error summary container. * @var string the default CSS class for the error summary container.
* @see errorSummary() * @see errorSummary()
*/ */
...@@ -239,6 +243,7 @@ class ActiveForm extends Widget ...@@ -239,6 +243,7 @@ class ActiveForm extends Widget
protected function getClientOptions() protected function getClientOptions()
{ {
$options = [ $options = [
'encodeErrorSummary' => $this->encodeErrorSummary,
'errorSummary' => '.' . implode('.', preg_split('/\s+/', $this->errorSummaryCssClass, -1, PREG_SPLIT_NO_EMPTY)), 'errorSummary' => '.' . implode('.', preg_split('/\s+/', $this->errorSummaryCssClass, -1, PREG_SPLIT_NO_EMPTY)),
'validateOnSubmit' => $this->validateOnSubmit, 'validateOnSubmit' => $this->validateOnSubmit,
'errorCssClass' => $this->errorCssClass, 'errorCssClass' => $this->errorCssClass,
...@@ -276,6 +281,7 @@ class ActiveForm extends Widget ...@@ -276,6 +281,7 @@ class ActiveForm extends Widget
public function errorSummary($models, $options = []) public function errorSummary($models, $options = [])
{ {
Html::addCssClass($options, $this->errorSummaryCssClass); Html::addCssClass($options, $this->errorSummaryCssClass);
$options['encode'] = $this->encodeErrorSummary;
return Html::errorSummary($models, $options); return Html::errorSummary($models, $options);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment