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 :)

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

报告相同问题?