Hi,

I have written a custom validator to make either first name or last name field in my form mandatory. But not both. I have added the custom validator to the lastName element. The validator won't run unless the element value is set. I want it to run even though no value is supplied to the element. Is it possible?

Here's the relevant part of the form:
[snip]
     $form = new Zend_Form;
     $firstName = $form->createElement('text', 'firstName')
->addValidator(new Zend_Validate_StringLength(0, 100))
                           ->setLabel('First Name');

       $middleName = $form->createElement('text', 'middleName')
->addValidator(new Zend_Validate_StringLength(0, 100))
                           ->setLabel('Middle Name');

       $lastName = $form->createElement('text', 'lastName')
->addValidator(new Zend_Validate_StringLength(0, 100))
                           ->setLabel('Last Name')
                           ->addValidator(new BV_Validate_PersonName);
[/snip]

Here's the code in BV_Validate_PersonName

[snip]
class BV_Validate_PersonName extends Zend_Validate_Abstract
{
   const MSG = 'failed';

   protected $_messageTemplates = array(
       self::MSG => 'Either first name or last name must be supplied',
   );

   public function isValid($value, $context = null)
   {
       $this->_setValue($value);
       if (is_array($context)) {
if ( (strlen($context['firstName']) > 1) or (strlen($context['lastName']) > 1)) {
               return true;
           }
       }
       $this->_error(self::MSG);
       return false;
   }
}

[/snip]

Regards,
--

With warm regards,
Sudheer. S
Business: http://binaryvibes.co.in, Tech stuff: http://techchorus.net, 
Personal: http://sudheer.net


Reply via email to