2

我在使用 Yii Fraework 方面非常陌生,我正在尝试使用来自 Yii Framework 的博客演示中的联系表单演示中的骨架来实现自定义表单。我做了与相应表单几乎完全相同的视图、控制器和模型,只是我得到以下 500 错误:

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

这里是: 控制器:

<?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; ?>

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

谢谢!

4

1 回答 1

2

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

您正在将视图文件作为参数传递给不起作用的 CForm。

我想这条线不需要:

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

请检查它是否有效:)

于 2013-02-09T14:25:44.210 回答