Siol
Siol

Reputation: 411

How to disallow character on with ZF2 Form Validator

I would like to disallow people to writing character "#" on my input how can I achieve it ?

Upvotes: 0

Views: 240

Answers (1)

Sam
Sam

Reputation: 16455

As @guessimtoolate suggested, since if you want to disallow only the sharp-character, then it's best to simply filter it out. Filters are run before Validation, therefore your workflow is like this:

  • Get Field Value
  • Remove all occurences of the character #
  • Validate your field using your attached validators

The Filter you wanna be using is the Zend\Filter\PregReplace. In case you're providing your Filters/Validators via the provider-interface, then the array-notation should be the following (it's untested, so you may work around a little bit with it)

'filters' => array(
    array('name' => 'Zend\Filter\PregReplace', 'options' => array(
       'pattern'     => '/#/',
       'replacement' => ''
    )
)

Upvotes: 1

Related Questions