简体   繁体   中英

Form Validation error on Yii Framework?

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

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'];

In Your Model class method rules first rule is invalid: array('judul, penulis') -> no validation rule specified. Try at least:

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

List of all Validation rules in Yii: http://www.yiiframework.com/wiki/56/

How validation works Parameters of a validator Choice of validators Scenarios Validation rules reference

boolean : CBooleanValidator

captcha : CCaptchaValidator

compare : CCompareValidator

date : CDateValidator

default : CDefaultValueValidator

email : CEmailValidator

exist : CExistValidator

file : CFileValidator

filter : CFilterValidator

in : CRangeValidator

length : CStringValidator

numerical : CNumberValidator

match : CRegularExpressionValidator

required : CRequiredValidator

safe : CSafeValidator

type : CTypeValidator

unique : CUniqueValidator

unsafe : CUnsafeValidator

url : CUrlValidator

Selected readings Built in validators in Yii....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM