0

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.

2
  • If this is the structure that will always be produced you could pretty easily use jQuery to add a class onto it. Commented Apr 6, 2014 at 16:18
  • I wanna know that how is it possible to add any htmlOption to the form element, not necessary a class or id. Commented Apr 6, 2014 at 16:23

1 Answer 1

1

In your view file before renderBegin make assignment for attributes:

<?php 
  $form->attributes= array('id'=>'el123', 'class'=>'qwe');
  echo $form->renderBegin(); ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.