Skip to content

Commit

Permalink
Fixes yiisoft#4225: Added ActiveForm::validateOnBlur and `ActiveFie…
Browse files Browse the repository at this point in the history
…ld::validateOnBlur`
  • Loading branch information
qiangxue committed Sep 3, 2014
1 parent 212c5ee commit f34b138
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Yii Framework 2 Change Log
- Fixed PBKDF2 key truncation.
- Adjusted API.
- Enh #4209: Added `beforeCopy`, `afterCopy`, `forceCopy` properties to AssetManager (cebe)
- Enh #4225: Added `ActiveForm::validateOnBlur` and `ActiveField::validateOnBlur` (qiangxue)
- Enh #4297: Added check for DOM extension to requirements (samdark)
- Enh #4317: Added `absoluteAuthTimeout` to yii\web\User (ivokund, nkovacs)
- Enh #4360: Added client validation support for file validator (Skysplit)
Expand Down
7 changes: 6 additions & 1 deletion framework/assets/yii.activeForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
encodeError: true,
// whether to perform validation when a change is detected on the input
validateOnChange: false,
// whether to perform validation when the input loses focus
validateOnBlur: false,
// whether to perform validation when the user is typing.
validateOnType: false,
// number of milliseconds that the validation should be delayed when a user is typing in the input field.
Expand Down Expand Up @@ -235,7 +237,10 @@
if (attribute.validateOnChange) {
$input.on('change.yiiActiveForm',function () {
validateAttribute($form, attribute, false);
}).on('blur.yiiActiveForm', function () {
});
}
if (attribute.validateOnBlur) {
$input.on('blur.yiiActiveForm', function () {
if (attribute.status == 0 || attribute.status == 1) {
validateAttribute($form, attribute, !attribute.status);
}
Expand Down
9 changes: 7 additions & 2 deletions framework/widgets/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ class ActiveField extends Component
*/
public $enableAjaxValidation;
/**
* @var boolean whether to perform validation when the input field loses focus and its value is found changed.
* @var boolean whether to perform validation when the value of the input field is changed.
* If not set, it will take the value of [[ActiveForm::validateOnChange]].
*/
public $validateOnChange;
/**
* @var boolean whether to perform validation when the input field loses focus.
* If not set, it will take the value of [[ActiveForm::validateOnBlur]].
*/
public $validateOnBlur;
/**
* @var boolean whether to perform validation while the user is typing in the input field.
* If not set, it will take the value of [[ActiveForm::validateOnType]].
Expand Down Expand Up @@ -717,7 +722,7 @@ protected function getClientOptions()
$inputID = Html::getInputId($this->model, $this->attribute);
$options['id'] = $inputID;
$options['name'] = $this->attribute;
foreach (['validateOnChange', 'validateOnType', 'validationDelay'] as $name) {
foreach (['validateOnChange', 'validateOnBlur', 'validateOnType', 'validationDelay'] as $name) {
$options[$name] = $this->$name === null ? $this->form->$name : $this->$name;
}
$options['container'] = isset($this->selectors['container']) ? $this->selectors['container'] : ".field-$inputID";
Expand Down
7 changes: 6 additions & 1 deletion framework/widgets/ActiveForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ class ActiveForm extends Widget
*/
public $validateOnSubmit = true;
/**
* @var boolean whether to perform validation when an input field loses focus and its value is found changed.
* @var boolean whether to perform validation when the value of an input field is changed.
* If [[ActiveField::validateOnChange]] is set, its value will take precedence for that input field.
*/
public $validateOnChange = true;
/**
* @var boolean whether to perform validation when an input field loses focus.
* If [[ActiveField::$validateOnBlur]] is set, its value will take precedence for that input field.
*/
public $validateOnBlur = true;
/**
* @var boolean whether to perform validation while the user is typing in an input field.
* If [[ActiveField::validateOnType]] is set, its value will take precedence for that input field.
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/framework/widgets/ActiveFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,14 @@ public function testGetClientOptionsClientValidation()
$actualValue = $this->activeField->getClientOptions();
$expectedJsExpression = "function (attribute, value, messages, deferred) {return true;}";
$expectedValidateOnChange = true;
$expectedValidateOnBlur = true;
$expectedValidateOnType = false;
$expectedValidationDelay = 200;

$actualJsExpression = $actualValue['validate'];
$this->assertEquals($expectedJsExpression, $actualJsExpression->expression);
$this->assertTrue($expectedValidateOnChange === $actualValue['validateOnChange']);
$this->assertTrue($expectedValidateOnBlur === $actualValue['validateOnBlur']);
$this->assertTrue($expectedValidateOnType === $actualValue['validateOnType']);
$this->assertTrue($expectedValidationDelay === $actualValue['validationDelay']);
}
Expand All @@ -288,13 +290,15 @@ public function testGetClientOptionsEnableAjaxValidation()
$actualValue = $this->activeField->getClientOptions();
$expectedJsExpression = "function (attribute, value, messages, deferred) {return true;}";
$expectedValidateOnChange = true;
$expectedValidateOnBlur = true;
$expectedValidateOnType = false;
$expectedValidationDelay = 200;
$expectedError = ".help-block";

$actualJsExpression = $actualValue['validate'];
$this->assertEquals($expectedJsExpression, $actualJsExpression->expression);
$this->assertTrue($expectedValidateOnChange === $actualValue['validateOnChange']);
$this->assertTrue($expectedValidateOnBlur === $actualValue['validateOnBlur']);
$this->assertTrue($expectedValidateOnType === $actualValue['validateOnType']);
$this->assertTrue($expectedValidationDelay === $actualValue['validationDelay']);
$this->assertTrue(1 === $actualValue['enableAjaxValidation']);
Expand All @@ -317,13 +321,15 @@ public function testGetClientOptionsValidatorWhenClientSet()
. "{ return 'yii2' == 'yii2'; }(attribute, value)) { return true; }}";

$expectedValidateOnChange = true;
$expectedValidateOnBlur = true;
$expectedValidateOnType = false;
$expectedValidationDelay = 200;
$expectedError = ".help-block";

$actualJsExpression = $actualValue['validate'];
$this->assertEquals($expectedJsExpression, $actualJsExpression->expression);
$this->assertTrue($expectedValidateOnChange === $actualValue['validateOnChange']);
$this->assertTrue($expectedValidateOnBlur === $actualValue['validateOnBlur']);
$this->assertTrue($expectedValidateOnType === $actualValue['validateOnType']);
$this->assertTrue($expectedValidationDelay === $actualValue['validationDelay']);
$this->assertTrue(1 === $actualValue['enableAjaxValidation']);
Expand Down Expand Up @@ -364,7 +370,7 @@ public function setClientOptionsEmpty($value)
}

/**
* Usefull to test other methods from ActiveField, that call ActiveField::getClientOptions()
* Useful to test other methods from ActiveField, that call ActiveField::getClientOptions()
* but it's return value is not relevant for the test being run.
*/
public function getClientOptions()
Expand Down

0 comments on commit f34b138

Please sign in to comment.