Page 1 of 1

Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 6:10 am
by jaoudestudios
I have a form in ZF and one of the form fields needs to match a code, otherwise I need it to fail. The code is the same for everyone so it will be hard coded in the form. The only way I see how to do this is by using a regex, but it just seems over kill. Is there a better way?

Code: Select all

 
$this->addElement('text', 'code', array(
            'label'      => 'Code',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                'alnum',
                array('regex', 'false', array('/aaa/')), // code to match
            )
        ));
 

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 6:59 am
by Eran
Somehow they never bothered to create a simple string comparison validator, even though it's mentioned in the documentation. You can implement one yourself, I have something similar in one of my projects:

Code: Select all

class Lionite_Validate_StringEquals extends Zend_Validate_Abstract
{
    const NOTEQUAL = 'equalString';
    protected $_messageTemplates = array(
        self::NOTEQUAL => "The two values do not match or are empty"
    );
    
    protected $_compare;
    public function __construct($compare) {
        $this -> _compare = $compare;
    }
    
    public function isValid($value){
        $this -> _setValue($value);
        if($value != $this -> _compare) {
            $this->_error(self::NOTEQUAL);
            return false;
        }
        return true;
    }
}

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 7:02 am
by jaoudestudios
Thanks pytrin :D

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 12:45 pm
by jaoudestudios
I need to use the regex example in another place, but how can I create a custom error message?

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 12:56 pm
by Eran
Use the 'messages' meta command:

Code: Select all

array('regex', 'false', array('/aaa/'),'messages' => 'Code is incorrect. Please try again')

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 1:04 pm
by jaoudestudios
Thanks for the quick reply.

Thats what I have, but cant seem to get that to work :?

Code: Select all

 
$this->addElement('text', 'code', array(
            'label'      => 'Code',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                                'alnum',
                array('regex', 'false', array('/aaaaa/i'), 'messages'=>'Error, please check your invitation'), // required code
            )
        ));
 

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 1:17 pm
by Eran
Is the regex validator the one failing? or maybe it is the alnum validator? what error message are you getting

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 1:20 pm
by jaoudestudios
It is the regex that is failing, because it is giving the error message 'asd' does not match against pattern '/aaaa/i'. I have now removed the alnum just incase, but still the same issue.

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 1:46 pm
by Eran
Sorry, I was using the Zend_Filter_Input syntax (I don't actually use Zend_Form). It seems Zend_Form has it's own methods to declare custom error messages - http://framework.zend.com/manual/en/zen ... ors.errors

Re: Zend Framework:: Form - matching a code

Posted: Mon May 04, 2009 11:52 pm
by jaoudestudios
Ah... I am still getting use to all the ways it is possible to do stuff.