Posts: 90
Threads: 30
Joined: Mar 2010
Reputation:
1
Jun 19th, 2010, 7:06 AM
Hi,
what is the easiest way to check if an validation error occured on a particular field?
I want to have something like this:
...
If error exists for email => show red input field for email
Else => show regular input field for email
If error exists for phone => show red input field for phone
Else => show regular input field for phone
...
Please advise me how to check for errors on only particular fields.
br,
Hannes
Posts: 2,456
Threads: 39
Joined: Dec 2008
Reputation:
6
Jun 19th, 2010, 12:57 PM
(This post was last modified: Jun 19th, 2010, 12:58 PM by Ben.)
Hi Hannes,
Good grief...! when I read your post I was like "oh sure, no problem" and looked at the code to find out the error return value. Turns out it only contains the error strings - not references to the error fields. So I'm afraid this can't be reliably done.
In my defense, I must have written that script over 8 years ago now.
What a pain....
If this is a show stopper for you, let me know and I'll update the script to (optionally!) return a hash of [error name field] => "error message" so you can find out which fields had errors.
It should really do it by default, but I have to worry about backward compatibility on people's systems.
Let me know!
- Ben
Posts: 90
Threads: 30
Joined: Mar 2010
Reputation:
1
Hey Ben,
thank you for your support!
Since it is possible to define individual error-messages for each rule, I will use them to track back the field where the error occured.
So in my example this would be:
PHP Code:
<?php
$rules[] = "required,Name,Error-Msg for Name";
$rules[] = "required,Email,Error-Msg for Email";
?>
...
<?php if (in_array("Error-Msg for Name", $errors)) {?>red mark<?php } else{?>regular<?php }?>
....
<?php if (in_array("Error-Msg for Email", $errors)) {?>red mark<?php } else{?>regular<?php }?>
That's good enough for the moment.
But thanks a lot for your offer!
Hannes.