CForm及其行为没有名为“beginWidget”的方法或闭包

时间:2013-02-08 09:31:33

标签: php yii

我是使用Yii Fraework的新手,我正在尝试使用来自Yii Framework的博客演示的联系表单演示中的skeletron实现自定义表单。我做了几乎完全相同的视图,控制器和模型作为相应的表单,只是我得到以下500错误:

Error 500 
CForm and its behaviors do not have a method or closure named "beginWidget".

以下是:Controller:

<?php
    class CustomController extends Controller {

        public function actionSubmit()
        {
            $model = new CustomForm;
            $form = new CForm('application.views.custom._form', $model);
            $this->pageTitle = "ffffffffffff";//['title'] = "Authentication";
            if($form->submitted('submit') && $form->validate())
                $this->redirect(array('blog/index'));
            else
                $this->render('_form', array('form'=>$form));
        }

        public function getGenders()
        {
            return array(
                0 => 'Male',
                1 => 'Female');
        }
    }
?>

模特:

<?php

    class CustomForm extends CFormModel {
        public $firstName;
        public $LastName;
        public $phone;
        public $address;
        public $gender;
        public $email;

        public function rules()
        {
            return array(
                    array('firstName, lastName, gender', 'required'),
                    array('email', 'email')
                );
        }
    }
?>

观点:

<?php
$this->pageTitle=Yii::app()->name . ' - Custom Form';
$this->breadcrumbs=array(
    'Custom Form',
);
?>

<h1>Custom Form</h1>

<?php if(Yii::app()->user->hasFlash('custom')): ?>

<div class="flash-success">
    <?php echo Yii::app()->user->getFlash('custom'); ?>
</div>

<?php else: ?>

<p>
If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
</p>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'custom-form',
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'firstName'); ?>
        <?php echo $form->textField($model,'firstName'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'lastName'); ?>
        <?php echo $form->textField($model,'lastName'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'email'); ?>
        <?php echo $form->textField($model,'email'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'phone'); ?>
        <?php echo $form->textField($model,'phone',array('size'=>60,'maxlength'=>128)); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'gender'); ?>
        <?php echo $form->radioButton($model,'gender',array('value'=>'Male')) . 'Male'; ?>
        <?php echo $form->radioButton($model,'gender',array('value'=>'Female')) . 'Female'; ?>
        <?php echo $form->error($model,'gender'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'address'); ?>
        <?php echo $form->textArea($model,'address',array('rows'=>6, 'cols'=>50)); ?>
    </div>

    <div class="row submit">
        <?php echo CHtml::submitButton('Submit'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

<?php endif; ?>

任何想法为什么我会收到此错误?我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

CForm表示包含表单输入规范的表单对象。

您正在将视图文件作为参数传递给无法正常工作的CForm。

我想这行不需要:

$form = new CForm('application.views.custom._form', $model);

请检查它是否有效:)