2

I'd like to add some extra validation to a Yii model.

I know it is simple enough to add in some logic to say a form field must be an exact length of 6 characters, however is it possible to set the rule to say the following:

if ($_POST['code'] == '')
    then no validation needed
else
    code string must be exactly 6 characters in length
lin
  • 17,956
  • 4
  • 59
  • 83
Zabs
  • 13,852
  • 45
  • 173
  • 297

3 Answers3

0

Go through this, you will get your answer, Yii validator

Sudhanshu Saxena
  • 1,199
  • 12
  • 32
0

You can use:

 public function rules() {
        return array(
            array('code', 'length', 'is' => 6, 'allowEmpty' => true)
        );
    }
NTkhan
  • 298
  • 1
  • 10
0

In your model add this rule , if code is an attribute

public function rules()
{
    return array(
       array('code', 'length', 'is'=>6, 'allowEmpty'=>true),          
    );
}

for more Info , Yii length validator

ramamoorthy_villi
  • 1,939
  • 1
  • 17
  • 40