Reputation: 344
Is it possible to use web2py validators like IS_NOT_EMPTY() in the controller? They seem to be imported but in the controller, however they are not useful at all. Which parameters should I use when calling them.
Upvotes: 1
Views: 678
Reputation: 25536
Validators are generally specified as the requires
attribute of DAL Field objects, typically when the models are defined. You can also specify the requires
attribute of a Field in a controller (this is typically done when validators or their arguments are conditional and not determined until a particular contoller action is called). When generating a FORM
object manually, you can also assign validators to the requires
attribute of the form helpers, such as INPUT
, SELECT
, etc.
You can also instantiate and call validators directly, though this is less common. To do so, first instantiate the validator object (possibly passing arguments if the validator constructor takes any), and then call the object by passing in a single value to be validated. The validator will return a 2-tuple -- the first element will be the validated value (possibly transformed if the validator does a transformation), and the second will be either None
or an error message (if validation failed). For example:
>>> IS_EMAIL()('bademail')
('bademail', 'enter a valid email address')
>>> IS_EMAIL()('[email protected]')
('[email protected]', None)
Upvotes: 5