zend-framework2zend-inputfilter

Check for unique constraint violation


I have 2 fields on my entity that form a unique constraint: fieldA and fieldB, mapped to database columns field_a and field_b, respectively.

My input filter requires both of the fields:

public function init()
{
    parent::init();

    $this->add([
        'name' => 'field_a',
        'required' => true,
        'allow_empty' => false,
    ]);

    $this->add([
        'name' => 'field_b',
        'required' => true,
        'allow_empty' => false,
    ]);
}

I am trying to figure out the best way to validate that those 2 fields are unique in the database table. The input filter will fail validation if there is already a different entity with those same field values.

I was thinking I would override the isValid function and put my custom logic in there.


Solution

  • I would suggest to use callback validator ( Zend\Validator\Callback ) on both fields and put your custom logic in the callback function.

    I would use InputFilter to add filters and validators to the form fields but you can implement InputFilterProviderInterface directly in the Form/Fieldset class.

    use Zend\InputFilter\InputFilter;
    
    class FormFilter extends InputFilter
    {
    
        public function __construct()
        {
           $this->add(
            array(
                'name' => 'field_a',
                'filters' => array(),
                'validators' => array (
                    array(
                        'name' => 'Zend\Validator\Callback',
                        'options' => array(
                            'messages' => array(
                                    \Zend\Validator\Callback::INVALID_VALUE => 'Custom Message',
                            ),
                            'callback' => array($this,'validateFieldA'),
                        ),
                    ), 
    
                )
            )
           );
    
          $this->add(
            array(
                'name' => 'field_b',
                'filters' => array(),
                'validators' => array (
                    array(
                        'name' => 'Zend\Validator\Callback',
                        'options' => array(
                            'messages' => array(
                                    \Zend\Validator\Callback::INVALID_VALUE => 'Custom Message',
                            ),
                            'callback' => array($this,'validateFieldB'),
                        ),
                    ), 
    
                )
            )
           );
    
        }
    
        public function validateFieldA($value, $context)
        {
            // $value contains the field_a value
            // $context['field_b'] contains the field_b value
    
            // put your custom logic here
            // return true if the fields are unique 
            // return false if the fields are not unique
        }
    
        public function validateFieldB($value, $context)
        {
              ....
        }
    
    
    }