Commit bbc7b076 by Qiang Xue

separate id and name of attribute for client side validation

parent 3a6adcd2
...@@ -51,6 +51,8 @@ ...@@ -51,6 +51,8 @@
}; };
var attributeDefaults = { var attributeDefaults = {
// a unique ID identifying an attribute (e.g. "loginform-username") in a form
id: undefined,
// attribute name or expression (e.g. "[0]content" for tabular input) // attribute name or expression (e.g. "[0]content" for tabular input)
name: undefined, name: undefined,
// the jQuery selector of the container of the input field // the jQuery selector of the container of the input field
...@@ -282,7 +284,7 @@ ...@@ -282,7 +284,7 @@
this.validate(this, getValue($form, this), msg); this.validate(this, getValue($form, this), msg);
} }
if (msg.length) { if (msg.length) {
messages[this.name] = msg; messages[this.id] = msg;
} else if (this.enableAjaxValidation) { } else if (this.enableAjaxValidation) {
needAjaxValidation = true; needAjaxValidation = true;
} }
...@@ -308,7 +310,7 @@ ...@@ -308,7 +310,7 @@
if (msgs !== null && typeof msgs === 'object') { if (msgs !== null && typeof msgs === 'object') {
$.each(data.attributes, function () { $.each(data.attributes, function () {
if (!this.enableAjaxValidation) { if (!this.enableAjaxValidation) {
delete msgs[this.name]; delete msgs[this.id];
} }
}); });
successCallback($.extend({}, messages, msgs)); successCallback($.extend({}, messages, msgs));
...@@ -345,11 +347,11 @@ ...@@ -345,11 +347,11 @@
} }
attribute.status = 1; attribute.status = 1;
if ($input.length) { if ($input.length) {
hasError = messages && $.isArray(messages[attribute.name]) && messages[attribute.name].length; hasError = messages && $.isArray(messages[attribute.id]) && messages[attribute.id].length;
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.name][0]); $error.text(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 {
...@@ -374,8 +376,8 @@ ...@@ -374,8 +376,8 @@
if ($summary.length && messages) { if ($summary.length && messages) {
$.each(data.attributes, function () { $.each(data.attributes, function () {
if ($.isArray(messages[this.name]) && messages[this.name].length) { if ($.isArray(messages[this.id]) && messages[this.id].length) {
$ul.append($('<li/>').text(messages[this.name][0])); $ul.append($('<li/>').text(messages[this.id][0]));
} }
}); });
$summary.toggle($ul.find('li').length > 0); $summary.toggle($ul.find('li').length > 0);
......
...@@ -673,6 +673,9 @@ class ActiveField extends Component ...@@ -673,6 +673,9 @@ class ActiveField extends Component
foreach ($this->model->getActiveValidators($attribute) as $validator) { foreach ($this->model->getActiveValidators($attribute) as $validator) {
/** @var \yii\validators\Validator $validator */ /** @var \yii\validators\Validator $validator */
$js = $validator->clientValidateAttribute($this->model, $attribute, $this->form->getView()); $js = $validator->clientValidateAttribute($this->model, $attribute, $this->form->getView());
if ($validator->whenClient !== null) {
$js = "if ({$validator->whenClient}(attribute, value)) { $js }";
}
if ($validator->enableClientValidation && $js != '') { if ($validator->enableClientValidation && $js != '') {
$validators[] = $js; $validators[] = $js;
} }
...@@ -689,7 +692,8 @@ class ActiveField extends Component ...@@ -689,7 +692,8 @@ class ActiveField extends Component
if ($enableClientValidation && !empty($options['validate']) || $enableAjaxValidation) { if ($enableClientValidation && !empty($options['validate']) || $enableAjaxValidation) {
$inputID = Html::getInputId($this->model, $this->attribute); $inputID = Html::getInputId($this->model, $this->attribute);
$options['name'] = $inputID; $options['id'] = $inputID;
$options['name'] = $this->attribute;
foreach (['validateOnChange', 'validateOnType', 'validationDelay'] as $name) { foreach (['validateOnChange', 'validateOnType', 'validationDelay'] as $name) {
$options[$name] = $this->$name === null ? $this->form->$name : $this->$name; $options[$name] = $this->$name === null ? $this->form->$name : $this->$name;
} }
......
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