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.
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.