When configuring a flexgid it is possible to define automatic validation by referring to one or more validators in the validations column. Validators are generally used when normal field validation available in html 5 is not sufficient to express the required validation rules. If the validation simply involves selection from a list of values then the select control can be used to force the user to enter one of the valid values. Also, html 5 allows you to indicate that field values must match a pattern indicated using a regular expression. This will also take the place of validators in many cases.

Even in cases where validation can be handled by one of these other means, you still might want to include a redundant validator since the validation logic is executed on the server and therefore can't be circumvented as is possible with client side (browser based) validation.

Specifing Multiple Validators

If a certain field requires more than one validator, enter the names of the validators on separate lines (by pressing the Enter key after specifying the first validator) as shown on the second row here.

Multiple validates must be entered on different lines

Notice that validators can accept parameters by specifying these in brackets following the validator name and separating parameters using commas.

Coding A Validator

Validators are implemented as php classes and should reside in the field_validator folder under the classes folder. Often these will be implemented within the pseudo site system so that the validator can be shared across multiple sites. If the validation rule is very specific to your application, you should implement this within your site directly since it is unlikely to be shared.

Validators should always extend field_validator_base which resides in system.

Supporting Parameters

If your validator accepts parameters, these should be defined as properties of the validator class and should be supported by a set method. Additionally, you must define a method called parameter_info which describes the validators parameters. Here is an example method that indicates that the validator accepts one parameter named pattern and it is an optional parameter of type string.

static function parameter_info() {
  return array('pattern' => array('required' => 'o', 'datatype' => 'string'));
}

The isvalid method

Every validator must support an isvalid method. This method will be passed a value and it should either return true (if the value is valid) or false (if the value is not valid). If the value is not valid, it should also assign a message to indicate why the value is not valid. Here is a sample isvalid method.

function isvalid($field_label,$field_value,$field_structure,$field_name,$field_occurrence=false) {
  if (!preg_match('/^'.$this->pattern.'$/',$field_value)) {
    $this->assign_message('!general',0002,array($field_label,$field_value),'Invalid :1: identifier value ":2:"',__LINE__.' '.__CLASS__);
    $this->assign_message_field($field_structure,$field_name,$field_occurrence);
    return false;
  }
  return true;
}

Notice that, in addition to the field value, the validator is also passed:

  • The label associated with the field (this can be used in error messages)
  • The name of the flexgrid where the value was entered
  • The field name
  • If the field is an array, the occurrence of the field will also be passed in.
Sample flexgrid definition
🡇
Sample rendered flexgrid