How to set up validation on form fields
All validation rules are saved in the structure_validations datatable. Each form field listed in the structure_fields can have zero-to-infinite validation rules assigned to it, with each rule being a row in the structure_validations datatable. The value of the structure_field_id field in the structure_validations datatable links each rule to the associated form field.
Contents |
Loading validations
Validations are built in two parts. The first part is built when a structure is set. All validations found in models related to that structures are cleared and then built with that new structure. That means that if you have many structures loaded in the same controller, the one to use for validation should be the last one loaded before the validate operation. Otherwise your validations won't be loaded properly.
The second part is made within the validation function. It sets a string length limit on each string field, based on their database length. This is in no way related to loading a structure, so that will always work, no matter the order in which structures were loaded.
structure_validations
rule
It's a TEXT field where you define this validation rule for the linked form field. A developer can place a regular expression, such as the ones supported in ATiM1, or use one of the supported data validation rules in CakePHP 1.3. If the latter, the rule should be entered in as a comma-separated text string, as the ATiM2 code will split that string on the commas to created the CakePHP 1.3 rule array.
on_action
IT can be set to either one of the following values: "update" or "create". This provides a mechanism that allows the specified rule to be applied either during the creation of a new record, or during update of an existing record.
language_message
It's the language alias, which will be swapped out for the language-specific error message appropriate for this rule. Each rule may only have one error message. If the validation rule is notEmpty, you don't need to provide a message. The system will automatically print this field is required. Should you still decide to provide a message, your message will be used.
Model validate array
Cake default validations imply creating a "validate" array into models. Currently, ATiM clears that array and recreates it with the validation rules found into the database. Hereby, any rule entered in the model validation array will be ignored.
Custom error
Should you want to manually generate an error message from a controller, you can call $this->{model}->validationErrors[] = {error message}. If you want to generate a message for a specific input field, you can call $this->{model}->validationErrors[{field}] = {error message}.
Validating a non existing model
It's possible to create structure fields that are not referring to database fields. To validate them, here is what's need to be done. We'll go with an example model called "MyModel" and a field called "my_field".
- Create the AppModel.
- Load the structure. The model need to be already created because the structure validations are applied on the model if it already exists.
- This is a required step if your field is a date. You need to add the field type to the "schema" of the table to allow autoaccuracy and deconstruct to run properly.
Other
There is currently no ATiM2 interface for accessing or managing these validations.
For more information, visit CakePHP 1.3's data validation documentation