Forum

Thread tagged as: Question, Problem, Addons

How do I display a dedicated error message, if string fails to validate

Hi Forum,
I am validating a form field against a specific string as described here:
https://forum.grabaperch.com/forum/07-15-2014-form-submit-required-validation-not-working?page=1#reply-21652
(Forget about my post in that thread. I figured it out.)
The only problem left is:
How do I display an error message, if the input string does not match the one to compare with?
For now I use

<perch:error for="all" type="general">[Error message here.]</perch:error>

But it would be nice to have a message appear besides the input field.

I am running Perch 2.8.8 and use the Forms app.

Nils Mielke

Nils Mielke 3 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Instead of for="all" use for="fieldID" to show errors for a specific field.

Drew McLellan said:

Instead of for="all" use for="fieldID" to show errors for a specific field.

I tried

<perch:error for="regcode" type="format">

and

<perch:error for="regcode" type="general">

and both do not appear when an incorrect string is entered. The form just doesn't get sent.
Validation works, though, as when I enter the correct string the form sends perfectly.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What type of validation are you performing? Can you show us the input tag?

The input field looks like this:

<perch:input type="text" required="true" id="regcode" label="Registrierungscode" helper="Summit::check_code" />

The helper function looks like this:

class Summit extends PerchAPI_Factory
{
    public static function check_code($regcode) {
        if ($regcode==="somecode") {
            return true;
        }

        return false;
    }
}
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, you're using a validation helper. So you should be able to use:

<perch:error for="regcode" type="helper"> ... </perch:error>

Worked like a charm.
Thank you, Drew.