【问题标题】:Yii - getActiveFormWidget() returns wrong type in core?Yii - getActiveFormWidget() 在核心中返回错误的类型?
【发布时间】:2012-07-08 16:27:45
【问题描述】:

我只是想创建一个非 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 -->

控制器动作:

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));    
        }
    }    
}

【问题讨论】:

  • 你有这个错误的堆栈跟踪吗?您是否尝试过在调试器中运行它以查看哪里出错了?

标签: php yii yii-cformmodel yii-cform


【解决方案1】:

嗯,我从来没有见过像你展示的那样使用 CForm。

我建议你使用活动表单小部件,

http://www.yiiframework.com/wiki/195/implementing-a-registration-process-using-the-yii-user-management-module/

并且您需要在 CFormModel 中定义所有这些字段。这样您就可以为它们提供适当的验证。

【讨论】:

  • 我展示的内容与此处记录的内容有什么区别? yiiframework.com/doc/guide/1.1/en/form.builder
  • 那是一篇很老的文章了,2011年1月发的。您肯定会遇到的缺点是您无法控制布局和设计。而且它没有被广泛使用,所以很少有人可以帮助你。您更应该使用 CFormModel 或 Active Record 方法。
【解决方案2】:

我知道答案真的晚了:) 但是为了其他可能有类似错误的人。

<?php
 // change from: echo $form;
 echo $form->render();
?>

我是单独渲染元素的,所以我是这样做的:

<?php
 // without the renderBegin() and renderEnd() it may give the no object error
 echo $form->renderBegin();
 echo $form->renderElements();
 echo $form->renderEnd();
?>

【讨论】:

    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多