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 に答える