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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵