Question

I have a form with many fields, 2 of which contain codes. I want to validate that the codes do not match. These are the elements:

            'code1' => array('text', array(
                    'required' => true,
                    'label' => 'form-label-code-1',
                    'filters' => array('StringTrim'),
                    'attribs' => array('placeholder' => 'Code 1'),
                    'decorators' => array(
                        'ViewHelper',
                        array('HtmlTag', array('tag' => 'div')),
                        'Errors'
                    ),
                    'validators' => array(
                        array('Callback', true, array(
                                'callback' => array($cservice, 'checkCodesUsed'),
                                'messages' => array(
                                    Zend_Validate_Callback::INVALID_VALUE => 'form-error-code-exists'
                                )))
                    )
                )),
            'code2' => array('text', array(
                    'required' => true,
                    'label' => 'form-label-code-2',
                    'filters' => array('StringTrim'),
                    'attribs' => array('placeholder' => 'Code 2'),
                    'decorators' => array(
                        'ViewHelper',
                        array('HtmlTag', array('tag' => 'div')),
                        'Errors'
                    ),
                    'validators' => array(
                        array('Callback', true, array(
                                'callback' => array($cservice, 'checkCodesUsed'),
                                'messages' => array(
                                    Zend_Validate_Callback::INVALID_VALUE => 'form-error-code-exists'
                                )))
                    )
                )),

The current callback just checks if the values already exist in the database. How do I specify a callback that has both code1 and code2's value? I can't seem to get my head around the zend documentation.

Was it helpful?

Solution

Solved it simply by adding a function in the form itself which acts as a callback.

public function checkCodesNotEqual() 
{

    if ($this->getElement('code1')->getValue() === $this->getElement('code2')->getValue()){
        return false;
    }

    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top