I was searching throughout the internet but couldn't find an answer to my problem. I am using the codeigniter form_validation helper.
I want to tell the user in which input field he made an error by coloring that input field for example red.
Here is an example how I do input fields
echo form_input($field, set_value($field));
and in the controller
$this->form_validation->set_rules($field, $field,'required|trim|xss_clean|callback_check_numbers');
$this->form_validation->set_message('check_numbers', '<script tpye="text/javascript">alert("Only numbers and dots are allowed");</script>');
Instead of that alert, I want to paint the input field red. But if I try to include some styling instead of that alert, nothing happens, because CI writes this before even putting header to the file, and then all error styling is lost with the include of css file. Is there a way to get the field id of the field where the error happened? And how could I paint the input the input red with for example, jquery?
Thank you.
what you are looking for is that "in the view page" :
<?php
if (form_error($field)) {
echo form_input($field, set_value($field), 'style="border:1px solid #ff0000"');
} else {
echo form_input($field, set_value($field));
}
?>
but i prefer you do not generate fields but write them down as html field, it would give you more flexibility manipulating your fields using inline php , and would be something like that :
<input name="field" <?php if (form_error('field')) {?> style="border:1px solid #ff0000" <?php } ?> type="text" value="<?php echo set_value('field'); ?>">