CodeIgniter Forums
Display form validation errors in an unordered list in a proper way. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Display form validation errors in an unordered list in a proper way. (/thread-37321.html)



Display form validation errors in an unordered list in a proper way. - El Forum - 01-05-2011

[eluser]Bramme[/eluser]
Hey everybody

I'm trying to display my form validation errors in a pretty way, but find it difficult to do this elegantly.

At the moment, I just do
Code:
$validation_errors = validation_errors('<li>', '</li>');
if ( ! empty($validation_errors))
{
    $data['message'] = '<ul>'.$validation_errors.'</ul>';
    $this->template->write_view('_feedback_message', $this->config->item('template_folder').'/partials/feedback_alert', $data);
}
But this feels incredibly sloppy. Having the html just there in my controller feels clunky, and cleaning it up by putting it all in a helper function, also doesn't ease my concious.

Any suggestions?


Display form validation errors in an unordered list in a proper way. - El Forum - 01-05-2011

[eluser]Victor Michnowicz[/eluser]
This is the code I place in my view:

Code:
&lt;?php if (validation_errors()): ?&gt;
    <ul>
        &lt;?php echo validation_errors('<li>', '</li>'); ?&gt;
    </ul>
&lt;?php endif; ?&gt;



Display form validation errors in an unordered list in a proper way. - El Forum - 01-05-2011

[eluser]Eric Barnes[/eluser]
Yep I do it the same as @elvicmic


Display form validation errors in an unordered list in a proper way. - El Forum - 01-05-2011

[eluser]Victor Michnowicz[/eluser]
Another thing I "discovered" recently was this:

Code:
$config = array(
    array(
        'field'   => 'name',
        'label'   => '<a href="#name">Name</a>',
        'rules'   => 'required'
    ),
    array(
        'field'   => 'email',
        'label'   => '<a href="#email">Email</a>',
        'rules'   => 'required'
    )
);

So now the error will include a link to the element that has an error (Assuming you name your inputs appropriately). You can even include some JavaScript (or :target) to help highlight the field.


Display form validation errors in an unordered list in a proper way. - El Forum - 01-05-2011

[eluser]Bramme[/eluser]
[quote author="elvicmic" date="1294281755"]This is the code I place in my view:

Code:
&lt;?php if (validation_errors()): ?&gt;
    <ul>
        &lt;?php echo validation_errors('<li>', '</li>'); ?&gt;
    </ul>
&lt;?php endif; ?&gt;
[/quote]

I guess I could do that, but that would mean I have to put the <ul> tags in my feedback message, which could also accept other things besides <li> tags.

At the moment I think I'll just start creating a buttload of helper functions.

The thing is that I'm putting html everywhere at the moment (my main library, my controller, my views), I'm not even comfortable with that. And if I have to start putting html in my views too...

I should also start using a language file now, while there aren't that many language strings littered everywhere yet.