From a982077c31e905a0835a582afe7d3ab62003631c Mon Sep 17 00:00:00 2001 From: Anatoly Rugalev Date: Tue, 16 Apr 2013 23:46:49 +0600 Subject: [PATCH 1/7] Added $trim property to CRequiredValidator --- framework/validators/CRequiredValidator.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/framework/validators/CRequiredValidator.php b/framework/validators/CRequiredValidator.php index cbabe5f1a..25352a3f4 100644 --- a/framework/validators/CRequiredValidator.php +++ b/framework/validators/CRequiredValidator.php @@ -40,6 +40,12 @@ class CRequiredValidator extends CValidator * This property is only used when {@link requiredValue} is not null. */ public $strict=false; + /** + * @var boolean whether it trims the value in comparing the string. + * When this is false, the attribute value can contain spaces at the beginning or end + * Defaults to true, meaning the value will be trimmed + */ + public $trim=true; /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. @@ -58,7 +64,7 @@ class CRequiredValidator extends CValidator $this->addError($object,$attribute,$message); } } - elseif($this->isEmpty($value,true)) + elseif($this->isEmpty($value,$this->trim)) { $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} cannot be blank.'); $this->addError($object,$attribute,$message); @@ -97,8 +103,12 @@ if(value!=" . CJSON::encode($this->requiredValue) . ") { $message=strtr($message, array( '{attribute}'=>$object->getAttributeLabel($attribute), )); + if($this->trim) + $emptyCondition = "jQuery.trim(value)==''"; + else + $emptyCondition = "value==''"; return " -if(jQuery.trim(value)=='') { +if({$emptyCondition}) { messages.push(".CJSON::encode($message)."); } "; From c9d2e4f5c84c5c0e5ae342223d3f8d3efc926107 Mon Sep 17 00:00:00 2001 From: Anatoly Rugalev Date: Wed, 17 Apr 2013 00:01:25 +0600 Subject: [PATCH 2/7] Added CRequiredValidator test --- .../validators/CRequiredlValidatorTest.php | 21 +++++++++++++++++++ .../validators/ValidatorTestModel.php | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 tests/framework/validators/CRequiredlValidatorTest.php diff --git a/tests/framework/validators/CRequiredlValidatorTest.php b/tests/framework/validators/CRequiredlValidatorTest.php new file mode 100644 index 000000000..7dd843f6f --- /dev/null +++ b/tests/framework/validators/CRequiredlValidatorTest.php @@ -0,0 +1,21 @@ +validate(array('username')); + $this->assertArrayHasKey('username', $model->getErrors()); + } + + public function testSpaces() + { + $model = new ValidatorTestModel('CRequiredValidatorTest'); + $model->username = ' '; + $model->validate(array('username')); + $this->assertArrayNotHasKey('username', $model->getErrors()); + } + +} diff --git a/tests/framework/validators/ValidatorTestModel.php b/tests/framework/validators/ValidatorTestModel.php index da39f6ffd..d6f949bf4 100644 --- a/tests/framework/validators/ValidatorTestModel.php +++ b/tests/framework/validators/ValidatorTestModel.php @@ -3,12 +3,14 @@ class ValidatorTestModel extends CFormModel { public $email; public $url; + public $username; public function rules() { return array( array('email', 'email', 'allowEmpty' => false, 'on' => 'CEmailValidatorTest'), array('url', 'url', 'allowEmpty' => false, 'on' => 'CUrlValidatorTest'), + array('username', 'required', 'trim' => false, 'on' => 'CRequiredValidatorTest'), ); } } From b12d5051712d7f0fd464f4f296ad80dd1f8be6de Mon Sep 17 00:00:00 2001 From: AnatolyRugalev Date: Wed, 17 Apr 2013 20:36:49 +0600 Subject: [PATCH 3/7] Added CRequiredValidator test case for $trim => true --- tests/framework/validators/CRequiredlValidatorTest.php | 8 ++++++++ tests/framework/validators/ValidatorTestModel.php | 2 ++ 2 files changed, 10 insertions(+) diff --git a/tests/framework/validators/CRequiredlValidatorTest.php b/tests/framework/validators/CRequiredlValidatorTest.php index 7dd843f6f..58b91d5d3 100644 --- a/tests/framework/validators/CRequiredlValidatorTest.php +++ b/tests/framework/validators/CRequiredlValidatorTest.php @@ -18,4 +18,12 @@ class CRequiredValidatorTest extends CTestCase $this->assertArrayNotHasKey('username', $model->getErrors()); } + public function testEmptyWithSpaces() + { + $model = new ValidatorTestModel('CRequiredValidatorTest'); + $model->address = ' '; + $model->validate(array('address')); + $this->assertArrayHasKey('address', $model->getErrors()); + } + } diff --git a/tests/framework/validators/ValidatorTestModel.php b/tests/framework/validators/ValidatorTestModel.php index d6f949bf4..30dcac90b 100644 --- a/tests/framework/validators/ValidatorTestModel.php +++ b/tests/framework/validators/ValidatorTestModel.php @@ -4,6 +4,7 @@ class ValidatorTestModel extends CFormModel public $email; public $url; public $username; + public $address; public function rules() { @@ -11,6 +12,7 @@ class ValidatorTestModel extends CFormModel array('email', 'email', 'allowEmpty' => false, 'on' => 'CEmailValidatorTest'), array('url', 'url', 'allowEmpty' => false, 'on' => 'CUrlValidatorTest'), array('username', 'required', 'trim' => false, 'on' => 'CRequiredValidatorTest'), + array('address', 'required', 'on' => 'CRequiredValidatorTest'), ); } } From 085c5abb353f68a72dd6391ed7476b902bf20385 Mon Sep 17 00:00:00 2001 From: AnatolyRugalev Date: Wed, 17 Apr 2013 20:37:05 +0600 Subject: [PATCH 4/7] Added changelog line --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index a3fa9c304..f57b3b8d9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -103,6 +103,7 @@ Version 1.1.13 work in progress - Enh #1596: Added CGridView::rowHtmlOptionsExpression to allow set HTML attributes for the row (Ryadnov) - Enh #1657: CDbCommandBuilder::createUpdateCounterCommand now can be used with float values (samdark, hyzhakus) - Enh #1658: CFormatter::formatHtml() is now more flexible and customizable through new CFormatter::$htmlPurifierOptions property (resurtm) +- Enh #2343: Added CRequiredValidator::$trim property whether to trim attribute value or not - Enh: Fixed the check for ajaxUpdate false value in jquery.yiilistview.js as that never happens (mdomba) - Enh: Requirements checker: added check for Oracle database (pdo_oci extension) and MSSQL (pdo_dblib, pdo_sqlsrv and pdo_mssql extensions) (resurtm) - Enh: Added CChainedLogFilter class to allow adding multiple filters to a logroute (cebe) From 8478152b74a402b07544aecbe476f07b9989ebfa Mon Sep 17 00:00:00 2001 From: AnatolyRugalev Date: Wed, 17 Apr 2013 20:40:33 +0600 Subject: [PATCH 5/7] Modified changelog line --- CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index f57b3b8d9..c76564d99 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -103,7 +103,7 @@ Version 1.1.13 work in progress - Enh #1596: Added CGridView::rowHtmlOptionsExpression to allow set HTML attributes for the row (Ryadnov) - Enh #1657: CDbCommandBuilder::createUpdateCounterCommand now can be used with float values (samdark, hyzhakus) - Enh #1658: CFormatter::formatHtml() is now more flexible and customizable through new CFormatter::$htmlPurifierOptions property (resurtm) -- Enh #2343: Added CRequiredValidator::$trim property whether to trim attribute value or not +- Enh #2343: Added CRequiredValidator::$trim property which determines to trim attribute value or not (KJLJon, AnatolyRugalev) - Enh: Fixed the check for ajaxUpdate false value in jquery.yiilistview.js as that never happens (mdomba) - Enh: Requirements checker: added check for Oracle database (pdo_oci extension) and MSSQL (pdo_dblib, pdo_sqlsrv and pdo_mssql extensions) (resurtm) - Enh: Added CChainedLogFilter class to allow adding multiple filters to a logroute (cebe) From 6ba4f1c2bb443b47a2a00c5ad4acce18d51b0af3 Mon Sep 17 00:00:00 2001 From: AnatolyRugalev Date: Wed, 17 Apr 2013 20:41:04 +0600 Subject: [PATCH 6/7] Added @since for $trim property --- framework/validators/CRequiredValidator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/validators/CRequiredValidator.php b/framework/validators/CRequiredValidator.php index 25352a3f4..52b13f037 100644 --- a/framework/validators/CRequiredValidator.php +++ b/framework/validators/CRequiredValidator.php @@ -44,6 +44,7 @@ class CRequiredValidator extends CValidator * @var boolean whether it trims the value in comparing the string. * When this is false, the attribute value can contain spaces at the beginning or end * Defaults to true, meaning the value will be trimmed + * @since 1.1.14 */ public $trim=true; /** From 3c2f5f1fbfa234d247b719a398ceda519adc4c7d Mon Sep 17 00:00:00 2001 From: AnatolyRugalev Date: Wed, 17 Apr 2013 22:30:48 +0600 Subject: [PATCH 7/7] changed changelog line --- CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index c76564d99..592212a4b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -103,7 +103,7 @@ Version 1.1.13 work in progress - Enh #1596: Added CGridView::rowHtmlOptionsExpression to allow set HTML attributes for the row (Ryadnov) - Enh #1657: CDbCommandBuilder::createUpdateCounterCommand now can be used with float values (samdark, hyzhakus) - Enh #1658: CFormatter::formatHtml() is now more flexible and customizable through new CFormatter::$htmlPurifierOptions property (resurtm) -- Enh #2343: Added CRequiredValidator::$trim property which determines to trim attribute value or not (KJLJon, AnatolyRugalev) +- Enh #2343: Added CRequiredValidator::$trim property which determines to trim attribute value or not (AnatolyRugalev) - Enh: Fixed the check for ajaxUpdate false value in jquery.yiilistview.js as that never happens (mdomba) - Enh: Requirements checker: added check for Oracle database (pdo_oci extension) and MSSQL (pdo_dblib, pdo_sqlsrv and pdo_mssql extensions) (resurtm) - Enh: Added CChainedLogFilter class to allow adding multiple filters to a logroute (cebe)