phpcssyiiyii-cform

add css class to the form (Yii form builder)


How can we add css class or id to the form element which has been created by Yii form bui Imagine that I have created a form as follows:

Controller:

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));
}

View

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

I want to set an id or a class for my form element which is generated by $form->renderBegin() function. I don't know how to add any html options to the form. Please someone help me.


Solution

  • In your view file before renderBegin make assignment for attributes:

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