0

So, i would like to change the style on a input element when i get a form_error on that element.

In my controller, i have all the form_validation rules, and every inputelement attributes

        if ($this->form_validation->run() == FALSE)
    {
        $data['username'] = array(
            'name' => 'username',
            'id' => 'username'
            );

        $data['password'] = array(
            'type' => 'password',
            'name' => 'password',
            'id' => 'password'
            );
    }

And in my View, i am printing them out.

            <li>
                <label for="username" class="bold">Username</label>
                <span class="input-prepend">$</span>
                <?php echo form_input($username); ?>
            </li>

So, i need the easiset way to change the class on the corrisponding element that gets a form error. Is it any easy way to do this?

Please, let me know if i'm explaining bad :) Thank you.

2 Answers 2

2

You can do something like this...

<li<?php echo (form_error('username') == '') ? '' : ' class="yes_error"'; ?>>
    <label for="username" class="bold">Username</label>
    <span class="input-prepend">$</span>
    <?php echo form_input($username); ?>
</li>

Output if there is error in username

<li class="yes_error">
    <label for="username" class="bold">Username</label>
    <span class="input-prepend">$</span>
    ....
</li>

And style it...

.yes_error {
    color: #FF0000;
    background: ...
    .....
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just a quick question; Is this the easiest way to do it? Let's say that i have 10 input elements. Do i have to write 10 if-clauses then? Or maybe create a function?
Yes, you can create a helper function check_error($error_data) { if(empty($error_data) { return; } else { return ' class="yes_error"'; } } and in form view you can do like this <li<?php echo check_error(form_error('username')); ?>></li>. But it is not much helpful.
0
<?php echo form_input($username, '', 'HERE your Css styles like style="" and javascript'); ?>

Why you not look the manual? http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

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.