Validators

Genropy provides a system that can validate data in order to avoid input error or mistyping. The validation is on the data bag and it starts at setValue time. Validation happens only if the value is changed or if the node is not valid.

It's possible to set a validation as attribute either in a widget or in a html structure or in a db structure. Note that the validator is set to the data bag and not to the widget or the db structure or the html structure.

Let's see an example:

form.input(datasource='path.example', validate_notNull='Y')

This means that in the data bag node path.example the value must exist. A validation implies the existence of a datasource.

Validator types

Here we list various types of validators:

validate_notNull

If it's set to 'Y' the value of the required data can't be null. If it's null it raises an error.

validate_case

Its possible values are:

  • upper : the required input must be uppercase, if not it's automatically converted to uppercase.
  • lower : the required input must be lowercase, if not it's automatically converted to lowercase.
  • capitalize : the required input must have the first letter set to uppercase, if not the first character is automatically converted to uppercase.

validate_len

Its value is a string "min,max" (e.g. "2,25") where "min" is the minimum lenght of the input data and max is the maximum lenght of the input data.

validate_inList

Its value is a string "element1,element2,element3,...,elementN" (e.g. "foo,bar,xyz"). The input data must be an element of the list, if not it's raised an error.

validate_db

Its value is a string "DbTable,DataBagPath" (e.g. "utils.cities,path.example.birthplace"). "DbTable" is the path of the table of the database considered for the validation; "DataBagPath" is the path of the bag where the data is written, it can be either a relative path or an absolute path. The input data must be a value present in the selected table.

validate_accept and validate_reject

Those are not poperly validators, but they're like triggers executed after the validation, respectively when the validation is accepted or when the validation is rejected.

(e.g. validate_reject="function onFailure(){alert("Validation rejected!!");}" )

validate_ext

It's a custom user-defined validation (in JavaScript). For example in a password changing form, there must be a validation when the password is retyped.

validate_server

It's similar to validate_ext, but it does a remote procedure call to the server (in Python). For example in a registration form, there must be a validation that controls if the username is already token.

execution order

Validators follows an execution order to optimize the performances. If a validation in the sequence fails the validation is stopped, else it goes on.

1. notnull

2. case

3. len

4. inlist

5. db

6. others:

  • ext
  • server