Forum

Thread tagged as: Question, Forms

Custom form validator for checking more than one field

Dear Perchers,

I am trying to put together a custom form validator for a form field that is required depending on whether another form field is checked. Is it possible to check two or more fields with a custom validator? Or the number of selected checkboxes? If not, what solution would you suggest?

This is the helper I am trying to use to make the title required if the abstract is poster:

<?php
    class Reg_Validator 
    {
        public static function check_abstract_title($abstract,$title) 
        { 
            if (($abstract=="poster") AND ($title=="")) { 
                return false;
            }  
            return true;
        }
    }
 ?>

Then in the form:

<perch:input id="abstract" type="radio" options="lecture,poster,none" />
<perch:input id="title" type="text" helper="Reg_Validator::check_abstract_title" />
<perch:error for="title" type="helper"><span class="error">You need to provide a title for the poster.</span></perch:error>

Thank you very much!

Laszlo

Laszlo Grama

Laszlo Grama 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

At the moment, only the current field gets passed to validators, so you'd probably need to inspect the environment directly. Obviously not ideal.

Thanks, Drew. Laszlo