zen of coding

Do I have any errors in my form?

A nice little trick to check if you have some errors in the view is to use the $session object.

Try this (in the view): pr($this->validationErrors) or pr($session->validationErrors), be sure to actually have some errors in the form…

I leave it up to you to come with some nice usage for it.

Well, here’s a little hint: try creating an element that will check if the above array is empty or set, and if so display some generic message, such as “Please fix the errors below…” this is especially useful for long HTML forms where the actual error message (or field error)¬†might fall below the screen fold.

  • I am trying to clear some validationErrors in a view, as I have 2 forms, and in the second one there is a field thats in the firstone. If the first one has been submitted, then the error msg will show in the second too!

    So to clear it I tried this:
    $form->validationErrors = $this->validationErrors = $html->validationErrors = $form->validates = null;

    doesnt work. Instead I did a CSS style display:none (if the first form was submitted) . Not ideal though. Any ideas, monsieur teknoidy?

  • @luke
    I don’t think clearing the errors in the view is sufficient, since the fields become “invalidated” at the model layer, and I believe the form helper builds them based on that and not just the error array, which I think is only used for display purposes (more or less).

    Without giving it too much thought (so take it with a grain of salt) :)… maybe you can decide which field is not needed to be passed to validation in beforeValidate() of your model.
    Or… write a model function in App Model … which will do the opposite of invalidate(), so you can manually override invalid fields as valid.

    Well, good luck either way ;)

  • This was actually useful in building an ErrorHelper. I needed to print out all the errors being thrown by validation but didn’t know how exactly to reference this. Great work :)

  • @Jose Diaz-Gonzalez

    Cool, glad it was helpful.

%d bloggers like this: