Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Make it possible to override the 'Value is required' message #11

Closed
larsnystrom opened this issue Aug 3, 2015 · 3 comments
Closed

Make it possible to override the 'Value is required' message #11

larsnystrom opened this issue Aug 3, 2015 · 3 comments

Comments

@larsnystrom
Copy link
Contributor

At the moment the 'Value is required' error message is hard coded into the validateInputs() method of BaseInputFilter. It would be very handy if it was possible to override that message, e.g. in an options array like in most classes in zend-validator.

@weierophinney
Copy link
Member

As noted on #12:

Currently, allow_empty and continue_if_empty are false by default, and required is true. This combination does injects a NotEmpty validator to the top of the ValidatorChain setup to break on error (i.e., if it fails, no more validators are executed); the validation chain is then executed, and, if the value is empty, the input is marked as invalid.

The solution to #11 is to keep the input as required, but to inject your own NotEmpty validator at the top of the chain yourself; Input will detect that it's present, and not inject its own NotEmpty validator.

So, the solution is:

  • Keep the input required
  • Inject your own NotEmpty validator with the custom message as the first validator in the chain.

@larsnystrom
Copy link
Contributor Author

Even if that solved my problem (which it doesn't, but I'll open a separate ticket for that see #14 ) I still think it'd be a good idea to be able to override the 'Value is required' message. As it stands, it is impossible to customize the message which makes it very hard to localize or improve UX by providing a more specialized description of what went wrong.

@marcuswinkler
Copy link

In the case of a required value which is missing from the input array, BaseInputFilter will never ask the Input instance to validate but will reject the element itself, setting the hard-coded error message.

So adding your own error message to the input can't work.

My workaround is this:

  • set required to false, to bypass the check in BaseInputFilter
  • set continue_if_empty to true, to actually hit the validators in Input
  • append a NotEmpty validator, to reject missing or empty values

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants