Highlight input-tag on error

March 15, 2007

Sometimes you need to highlight a specific input-tag when the validation returns an error, like changing the background or creating a red border around it.

Until now i used this code to add a custom css-class if a error was found:

if ($sf_request->hasError('email'))
  echo input_tag('email',null,array('class' => 'errorbg'));
  echo input_tag('email');


But today someone in #symfony asked how i would do it, so i showed him my code and had to admit that this wasn’t a nice solution, so i just reviewed the code a bit resulting in this one-liner:

echo input_tag('email', null, $sf_request->hasError('email') ? 'class=errorbg' : null);

Have fun!


6 Responses to “Highlight input-tag on error”

  1. superhaggis Says:

    Alternatively, if you don’t like NULLs:

    hasError(’email’) ? array(‘class’ => ‘errorbg’,) : array()) ?>

  2. superhaggis Says:

    Hmm, it swallowed my comment. :p

    echo input_tag(’email’, ”, $sf_request->hasError(’email’) ? array(‘class’ => ‘errorbg’,) ? array());

  3. iamian Says:

    I would suggest a custom helper. This would save you loads of code

  4. bleach Says:

    Do you have the errorbg class you mentioned ? And your code works with the client side validation plugin ?

  5. symfoniac Says:

    bleach, the errorbg-class is just a css-class and up to you.

    I haven’t tried the client side validation plugin yet.

  6. Markus Says:

    of course, a custom helper would be nice.

    But maybe there is a hook in the input tag function which you can use (sfMixer,..)

    If not, maybe ask fabien to register a hook in the global tag() function so you could do global stuff with one little hook,..


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: