Welcome Guest, Not a member yet? Register   Sign In
Beginner - How to position and style form_error messages
#1

[eluser]tmfl[/eluser]
Hi,

I'm new to CodeIgniter and I'm trying to find out how I can position the form_error validation messages.

At the moment I have the following in my Controller

Code:
...
$this->load->library('form_validation');
  $this->form_validation->set_error_delimiters('<label class="error">','</label>');
  $this->form_validation->set_message('required', 'Required Field');
......

In my view I've got

Code:
....
<p>
    <label for="firstname">First Name</label>&lt;input type="text" name="firstname" value="&lt;?php echo set_value('firstname');?&gt;" size="20" /&gt;&lt;?php echo form_error('firstname'); ?&gt;"
</p>
....

This works up to a point....When I submit the form with the firstname field left blank I get an error message after the input field. Using CSS the error message is red producing the following

Label (First Name) - Empty Input Box - Red error message (Required Field)

My understanding was that the colour of the label text would change to red ....Why is it creating another label ?

Secondly, if I want to change the position of the error message to inside the input box, how do I achieve this ?

thanks in advance

tmfl

#2

[eluser]boltsabre[/eluser]
Quote:My understanding was that the colour of the label text would change to red ....Why is it creating another label ?
Because you are echoing it out via:
Code:
&lt;?php echo form_error('firstname'); ?&gt;
So you've got your first label hard coded, followed by your input, and if validation fails, you're also echoing out the second label. Just use a span or div instead if you'd prefer?
Code:
$this->form_validation->set_error_delimiters('<div class="error">','</div>')

Quote:Secondly, if I want to change the position of the error message to inside the input box, how do I achieve this ?
I'd actually advise against this method, it's not very user friendly... you'll override what the user entered. They won't be able to see what they entered the first time round to see why they got the error message. It would be okay for just this for with just a "required" validation, but I'd steer clear of it anyway. I'd just echo them out either under your inputs that fail, or one message per line either at the top or bottom of the form.
But I think this would do it (maybe, im at work, not at home, I can't check atm) if you really want to go down that road:
Code:
//remove this line of code from your controller
$this->form_validation->set_error_delimiters('<div class="error">','</div>')
Code:
value="&lt;?php echo set_value(form_error('firstname'));?&gt;"
#3

[eluser]tmfl[/eluser]
thanks for the reply....
thats interesting about the second part of my question....guess thats true....

so should i use codeigniter to generate the form label and then this will change the label text colour to red ?

thanks again....

#4

[eluser]aquary[/eluser]
I suggest using other elements for making an error message, p or span do the best job for me since most of the time I have a specific style for form labels.




Theme © iAndrew 2016 - Forum software by © MyBB