gopal
gopal

Reputation: 15

Yii custom validation

I have a model with a custom validation

class RegisterForm extends CFormModel
{

   public $codiceMembro;
   //...some more attributes

   //validation
   public function rules()
   {
     return array(
       array('codiceMembro', 'codiceODataNascita'),
       //...some more rules
     );
   }


   // Declares attribute labels.
   public function attributeLabels()
   {
     return array(
    'codiceMembro'=>"a description",
        //...some more labels
     );
   }

   //My own custom validate function, always error for it
   public function codiceODataNascita($attribute, $params){   
      $this->addError('codiceMembro','a bad message');  
   }

   //...other model stuff
}

Then in the view, here how I insert the model

<?php
$model=new RegisterForm;
$form=$this->beginWidget('CActiveForm', array(
    'id'=>'register-form',
    'enableClientValidation'=>true,
    'clientOptions'=>array(
    'validateOnSubmit'=>true,
    ),
));
?>
<div class="row">
  <?php echo $form->label($model, 'codiceMembro'); ?>
  <?php echo $form->textField($model, 'codiceMembro');?>
  <?php echo $form->error($model,'codiceMembro'); ?>
</div>
//...so on till the end of the code

What I expect is that whatever I type, I obtain error message

Instead for this code everything is validated as OK

Upvotes: 0

Views: 7504

Answers (1)

jmarkmurphy
jmarkmurphy

Reputation: 11473

If you want your custom validator to support client validation you need to create a custom validator class by extending CValidator. Here is a wiki page detailing how to do this: http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/#hh1

Upvotes: 3

Related Questions