将css类添加到表单(Yii表单生成器)

时间:2014-04-06 16:16:53

标签: css yii yii-cform

我们如何将css类或id添加到由Yii form bui创建的表单元素中 想象一下,我创建了一个表单如下:

控制器:

public function actionRegistration()
{

    $form = new CForm('application.views.user.registerForm');
    $form['user']->model = new Users;
    $form['profile']->model = new Profile;

    if(isset($_POST['Users']))
    {
        $form['profile']->model->attributes = $_POST['Profile'];
        $_POST['Users']['mobile'] = $_POST['Profile']['mobile'];
        $form['user']->model->attributes = $_POST['Users'];

        if($form->validate()) {
            $user = $form['user']->model;
            $profile = $form['profile']->model;
            if($profile->save(false))
            {
                $user->profile_id = $profile->id;
                $user->save(false);
            }
        }
        else {
            $errors = array_merge($form['profile']->model->errors, $form['user']->model->errors);
        }
    }

    $this->render('registration', array('form'=>$form));
}

查看

    <?php echo $form->renderBegin(); ?>
    <div class="row">
        <div class="nine columns">
            <div class="field">
                <?php echo $form['profile']['fname']; ?>
            </div>
            <div class="field">
                <?php echo $form['profile']['lname']; ?>
            </div>
            <div class="row">
                <div class="eight columns">
                    <div class="field phone">
                        <?php echo $form['profile']['phone']; ?>
                    </div>
                </div>
                <div class="eight columns">
                    <div class="field mobile">
                        <?php echo $form['profile']['mobile']; ?>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="eight columns">
                    <div class="field password">
                        <?php echo $form['user']['pass']; ?>
                    </div>
                </div>
            </div>

            <div class="field address email">
                <input type="text" name="Users[email]" value="" placeholder="Email" class="text input">
            </div>
        </div>


    </div>
    <div class="row text-center margins">
        <span class="medium warning btn">
            <button type="submit"Submit</button>
        </span>
    </div>
    <?php echo $form->renderEnd(); ?>

我想为我的表单元素设置一个id或类,它由$ form-&gt; renderBegin()函数生成。我不知道如何在表单中添加任何html选项。请有人帮助我。

1 个答案:

答案 0 :(得分:1)

在renderBegin为属性赋值之前的视图文件中:

<?php 
  $form->attributes= array('id'=>'el123', 'class'=>'qwe');
  echo $form->renderBegin(); ?>