2
votes

I'm using Zend Framework 2.1

I want to create a blocklist of domains or email addresses for the email field of a Zend Form. I think it is possible to do with Zend InputFilter. But i don't know how to do it

1

1 Answers

0
votes

InputFilter are intended to validate and sanitize a group of input data, they use Validators and Filters to achieve this goal.

What you need to block some domains is a custom validator.

The tricky part here is that you need to extract the domain from the email address, thing that the EmailAddress validator is already doing (and it does it well).

Fortunately, EmailAddress validator use an internal Hostname validator to check that the domain is valid.

Here is what I suggest :

  • Write your own validator, extending Zend\Validator\Hostname. If you overload the isValid() method, you'll be able to add your custom blocking logic to the validator... don't forget to return the parent method value after checking your own logic.
  • Configure an EmailAddress validator to use your custom Hostname validator (using the setHostnameValidator() method, or by passing the configuration in the constructor).
  • Use your EmailAddress validator in an InputFilter, see the documentation for more details in how to do this : http://framework.zend.com/manual/2.2/en/modules/zend.input-filter.intro.html

Hope this helps.