[eluser]TheFuzzy0ne[/eluser]
I like to show the error message in the label for the field, and I found that I spend a lot of time writing complex looking lines of code in my view, that ended up looking a lot like this.
Code:
<label <?=(form_error('form_field')) ? 'class="formError" ' : ''?>for="form_field">URL: <?=form_error('form_field')?></label>
Now I have a helper:
Code:
<?php
/**
*
*
* @param string $label_text The text to display in the label
* @param string $field_name The field name of the form input this label relates to
* (assumes you've set the form input id to the specified field name).
* @return string
*/
function get_label($label_text, $field_name)
{
$label = "<label for=\"$field_name\"";
$label .= (form_error($field_name)) ? ' class="formError"' : '';
$label .= ">" . $label_text . ":";
$label .= (form_error($field_name)) ? " " . form_error($field_name) : '';
$label .= "</label>\n";
return $label;
}
// End of file: MY_Form_helper.php
// Location: ./system/application/helpers/MY_Form_helper.php
and I call it from within the view like this:
Code:
<?php echo get_label('Field Title', 'field_name'); ?>
There may be simpler ways to do this, but this method works for me.
I'm sure you can modify the function to suit your needs.