doulai8128 2014-02-18 19:02
浏览 41
已采纳

Yii Cactive表格

Hi I want to create a contact form in YII view. i am using CACtive forms but the problem is it requires a model, I don't have a model, I am just calling a view without any model. below is my code

$form = $this->beginWidget('CActiveForm', array(
        'id'=>'fundraising-form',
        'enableAjaxValidation'=>false,
        'clientOptions'=>array(
        'validateOnSubmit'=>true,
        'hideErrorMessage'=>true,
    ),
        'enableClientValidation'=>true,
         'focus'=>array($model,'name'),
            )); 
  echo $form->labelEx('name_of_organization');

gives me the error Missing argument 2 for CActiveForm::labelEx(),

  • 写回答

1条回答 默认 最新

  • doucai6663 2014-02-18 19:36
    关注

    Well, if I guess correctly, you don't have any table to store your data and you do not need any interaction between your model and database, and you just want to use ActiveForm features without a model. It's impossible. But there is a trick to solve this problem. You can easily create a fake model and use it into your ActiveForm.

    First, Create a model in your models directory, BUT NOT AN ORDINARY MODEL. Like below:

    class FakeModel extends CFormModel{
    public $organizationName; //for example!
    public $fullname; // for example!!
    public $email; // for example!!!
    public function rules() {
        return array(
            array('email','email'),
            array('fullname,organizationName','required')
        );
    }
    
    public function attributeLabels() {
        return array(
            'email'=>'E-Mail Address',
            'organizationName'=>'Organization Name',
            'fullname'=>'Full Name',
        );
    }
    }
    

    Note that, FakeModel extended the CFormModel class.

    Umm, Now you have a model just like other AR's models :) You can send this model into your ActiveForm, even perform validation in your model.

    $fakeModel=new FakeModel();
    //for validation
    if($fakeModel->validate()){
     // SEND EMAIL FOR EXAMPLE
    }
    

    As you can see, there is no interaction between your ActiveForm and database. Easily pass your $fakeModel as the model(second parameter) in your CActiveForm.

    UPDATE As you probably know, the real name is 'CFormModel'.

    I hope it help :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用