Zend Framework:: Form - matching a code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Zend Framework:: Form - matching a code

Post 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
            )
        ));
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Zend Framework:: Form - matching a code

Post 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;
    }
}
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Zend Framework:: Form - matching a code

Post by jaoudestudios »

Thanks pytrin :D
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Zend Framework:: Form - matching a code

Post by jaoudestudios »

I need to use the regex example in another place, but how can I create a custom error message?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Zend Framework:: Form - matching a code

Post by Eran »

Use the 'messages' meta command:

Code: Select all

array('regex', 'false', array('/aaa/'),'messages' => 'Code is incorrect. Please try again')
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Zend Framework:: Form - matching a code

Post 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
            )
        ));
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Zend Framework:: Form - matching a code

Post by Eran »

Is the regex validator the one failing? or maybe it is the alnum validator? what error message are you getting
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Zend Framework:: Form - matching a code

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Zend Framework:: Form - matching a code

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Zend Framework:: Form - matching a code

Post by jaoudestudios »

Ah... I am still getting use to all the ways it is possible to do stuff.
Post Reply