diff --git a/CHANGELOG b/CHANGELOG index 31357d5fd..be4f33d65 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -175,6 +175,7 @@ Version 1.1.13 December 30, 2012 - 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 (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) diff --git a/framework/validators/CRequiredValidator.php b/framework/validators/CRequiredValidator.php index 21e0e18bd..4d0e42ffe 100644 --- a/framework/validators/CRequiredValidator.php +++ b/framework/validators/CRequiredValidator.php @@ -40,6 +40,13 @@ 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 + * @since 1.1.14 + */ + public $trim=true; /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. @@ -58,7 +65,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 +104,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)."); } "; diff --git a/tests/framework/validators/CRequiredlValidatorTest.php b/tests/framework/validators/CRequiredlValidatorTest.php new file mode 100644 index 000000000..58b91d5d3 --- /dev/null +++ b/tests/framework/validators/CRequiredlValidatorTest.php @@ -0,0 +1,29 @@ +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()); + } + + 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 59f40db43..c7604cd59 100644 --- a/tests/framework/validators/ValidatorTestModel.php +++ b/tests/framework/validators/ValidatorTestModel.php @@ -11,6 +11,9 @@ class ValidatorTestModel extends CFormModel public $number; + public $username; + public $address; + public function rules() { return array( @@ -26,6 +29,9 @@ class ValidatorTestModel extends CFormModel array('url', 'url', 'allowEmpty'=>false, 'on'=>'CUrlValidatorTest'), array('number', 'numerical', 'min'=>5, 'max'=>15, 'integerOnly'=>true, 'on'=>'CNumberValidatorTest'), + + array('username', 'required', 'trim' => false, 'on' => 'CRequiredValidatorTest'), + array('address', 'required', 'on' => 'CRequiredValidatorTest'), ); } }