1

I want to create a form field for a login script that allows either a username or an email address to be entered.

I have a validator

$loginName = new Zend_Form_Element_Text('loginName');
$loginName->addValidators(array('Alnum', 'EmailAddress');

Which does not work (because it seems to validate Alnum AND EmailAddress). I would like to validate Alnum OR EmailAddress.

Any suggestions on how to do this?

1 Answer 1

5

You have two options. You can either use a Regex validator, or you can write a custom validator which itself uses the Alnum and Email validators.

http://framework.zend.com/manual/en/zend.validate.writing_validators.html

The second option would look something like this:

    class My_Validate_Field extends Zend_Validate_Abstract
    {
     
        protected $_messageTemplates = array(
            'alnumemail' => "'%value%' is not an alphanumeric string or email address"
        );
     
        public function isValid($value)
        {
              $alnum = new \Zend_Validate_Alnum();
              $email = new \Zend_Validate_EmailAddress();

        $valid = $alnum->isValid( $value ) || $email->isValid( $value);

        if( !$valid ) {
            $this->_error();
        }

            return $valid;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.