将 css 类添加到表单(Yii 表单构建器)

问题描述 投票:0回答:1

我们如何向 Yii form bui 创建的表单元素添加 css class 或 id 想象一下我创建了一个如下的表单:

控制器:

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

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

php css yii yii-cform
1个回答
1
投票

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

<?php 
  $form->attributes= array('id'=>'el123', 'class'=>'qwe');
  echo $form->renderBegin(); ?>
© www.soinside.com 2019 - 2024. All rights reserved.