dongxian0320 2014-04-07 08:16
浏览 29

Yii框架上的表单验证错误?

I'm new on Yii, I have Try Crud and succes. now try to create validation but Still error.

here my script Model:Buku.php

public function rules()
    {
        return array(
            array('judul, penulis'),
            array('judul', 'length','max'=>50),
            array('penulis', 'length', 'max'=>50),
            array('judul,penulis', 'on'=>'search'),
        );       

    }

Controller: BukuController.php

public function actionCreate()
    {
        $model = new Buku;
        if(isset($_POST['Buku']))
        {
            $model->judul    =$_POST['Buku']['judul'];
            $model->penulis  =$_POST['Buku']['penulis'];
            $model->save();

            /*if($model->save())
            {
                Yii::app()->user->setFlash('Succes', "Data berhasil Disimpan");
                $this->redirect(array('index'));
            }*///end of

        }//end if isset
        $this->render('create',array('model'=>$model));
    }//end of class

View: create.php

<div class="form">

    <h2>Add Data</h2>
    <?php echo CHtml::beginForm(array('buku/create'));?>

    <?php 

    echo CHtml::errorSummary($model);
    ?>
    <div class="row">
        <?php echo CHtml::activeLabel($model,'judul');?>
        <?php echo CHtml::activeTextField($model,'judul','');?>
        <?php echo CHtml::errorSummary($model,'judul');?>
    </div>
    <div class="row">
        <?php echo CHtml::activeLabel($model,'penulis');?>
        <?php echo CHtml::activeTextField($model,'penulis','');?>
        <?php echo CHtml::errorSummary($model,'penulis');?>
    </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton('Submit');?>
        <?php echo CHtml::endForm();?>
    </div>

</div>

the Error MEssage is

Buku has an invalid validation rule. The rule must specify attributes to be validated and the validator name

Anyone can Help This? Im very appreciated Your answer. Thanks

  • 写回答

3条回答 默认 最新

  • donqh00404 2014-04-07 08:20
    关注

    Pleas edit the first line, probably you wanted there 'required'

    array('judul, penulis','required'),
    

    And this is not needed:

    $model->judul    =$_POST['Buku']['judul'];
    $model->penulis  =$_POST['Buku']['penulis'];
    

    instead you can use the mass-assignment: http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/

    $model->attributes=$_POST['Buku'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算