0

我有一个带有自定义验证的模型

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
}

然后在视图中,在这里我如何插入模型

<?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

我期望的是无论我输入什么,我都会收到错误消息

相反,对于此代码,一切都被验证为 OK

4

1 回答 1

3

如果您希望自定义验证器支持客户端验证,则需要通过扩展 CValidator 创建自定义验证器类。这是一个详细说明如何执行此操作的 wiki 页面:http ://www.yiiframework.com/wiki/168/create-your-own-validation-rule/#hh1

于 2013-08-02T13:05:19.990 回答