如何在YII中制作验证模型

时间:2015-02-11 05:53:12

标签: yii model

这是我的模特

<?php
class EmailForm extends CModel 
{
    public $firstname;
    public $lastname;
    public $contactno;
    public $email;
    public $subject;
    public $body;

    public function rules()
    {
        return array(
            array('firstname, lastname, contactno, email, subject, body', 'required'),
            array('email', 'email'),            
        );
    }

}

仅用于验证我制作了这个模型。 但它显示错误:

Fatal error: Class EmailForm contains 1 abstract method and
 must therefore be declared abstract or implement the 
remaining methods (CModel::attributeNames)

对于CModel我必须使用什么?

2 个答案:

答案 0 :(得分:0)

如错误所示,您需要在自定义模型中实现attributeNames()功能,如:

class EmailForm extends CModel 
{
    public $firstname;
    public $lastname;
    public $contactno;
    public $email;
    public $subject;
    public $body;

    public function rules()
    {
        return array(
            array('firstname, lastname, contactno, email, subject, body', 'required'),
            array('email', 'email'),            
        );
    }

    public function attributeNames(){
        return array(
            'firstname',
            'lastname',
            'contactno',
            'email',
            'subject',
            'body'
        );
    }

}

答案 1 :(得分:0)

如我所见,您想编写一个FormModel,那么为什么不延长CFormModel

CModel具有抽象方法,这些方法在CFormModel - 类中实现。