如何在 YII 中制作用于验证的模型
How to make make model for validation in YII
这是我的模型
<?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,我必须使用什么?
如错误提示,您需要在自定义模型中实现 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'
);
}
}
如我所见,您想编写一个 FormModel,那么为什么不扩展 CFormModel
。
CModel
有抽象方法,在CFormModel
-Class.
中实现
这是我的模型
<?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,我必须使用什么?
如错误提示,您需要在自定义模型中实现 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'
);
}
}
如我所见,您想编写一个 FormModel,那么为什么不扩展 CFormModel
。
CModel
有抽象方法,在CFormModel
-Class.