我只是想创建一个非 AJAX 注册表单。
当我通过 CForm 提交时,PHP 说 error() 正在非对象上调用。我检查了发生错误的源文件,并(使用 var_dump(get_class($parent->getActiveFormWidget()));
)发现 getActiveFormWidget() 返回一个 CFormInputElement
,没有定义 error()。
我认为这与CForm::activeForm有关,但默认为 CActiveForm
。
我确实尝试了'enableAjaxValidation'=>false
。
我不明白什么?我怀疑我一路上误解了一些东西。
CFormInputElement::renderError():
public function renderError()
{
$parent=$this->getParent();
// $parent->getActiveFormWidget() returns object that does not define error()
return $parent->getActiveFormWidget()->error($parent->getModel(), $this->name, $this->errorOptions, $this->enableAjaxValidation, $this->enableClientValidation);
}
型号:
class RegisterFormModel extends CFormModel {
public $email;
public $password;
public $password_confirm;
public function rules()
{
return array(
array('email, password, password_confirm', 'required'),
array('password','compare','compareAttribute'=>'password_confirm'),
array('password, password_confirm','length','min'=>'6','max'=>'20'),
array('email', 'email'),
array('email', 'length', 'max'=>256)
);
}
public function attributeLabels()
{
return array(
'email'=>'Email',
'password'=>'Password',
'password_confirm'=>'Confirm Password',
);
}
}
查看:
<div class="form">
<?php echo $form; ?>
</div><!-- form -->
Controller 操作:
class RegisterAction extends CAction
{
public function run()
{
$register_model = new RegisterFormModel;
$controller = $this->getController();
$form = new CForm(array(
'title'=>'Register',
'enableAjaxValidation'=>false,
'elements'=>array(
'email'=>array(
'type'=>'text',
'maxlength'=>256,
),
'password'=>array(
'type'=>'password',
'minlength'=>6,
'maxlength'=>32,
),
'password_confirm'=>array(
'type'=>'password',
'minlength'=>6,
'maxlength'=>32,
),
),
'buttons'=>array(
'register'=>array(
'type'=>'submit',
'label'=>'Register',
),
),
), $register_model);
if($form->submitted('register', true) && $form->validate())
{
// ...
}
else
{
$controller->render('register', array('model'=>$register_model, 'form'=>$form));
}
}
}
最佳答案
嗯,我从未见过像您向我们展示的那样使用 CForm。
我建议您使用事件表单小部件,
并且您需要在 CFormModel 中定义所有这些字段。这样您就能够为它们提供适当的验证。
关于php - Yii - getActiveFormWidget() 在核心中返回错误类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11384733/