Version: current

# Class Validator

This validation class handles all form and custom form validation through the use of Required fields.

Relies on javascript for client-side validation, and marking fields after serverside validation.

Acts as a visitor to individual form fields.

  Object
  Validator

### Direct known subclasses

`RequiredFields`

### Indirect known subclasses

`CustomRequiredFields`, `Member_Validator`

Abstract
Package: forms\validators
Located at sapphire/forms/Validator.php

## Methods summary

`public static` | # `set_javascript_validation_handler( mixed $handler )` Call this function to set the javascript validation handler for all valdiation on your site. This could be called from _config.php to set site-wide javascript validation, or from ContentController::init() to affect only the front-end site. Use instance method `Validator::setJavascriptValidationHandler()` to only set handler for a specific form instance. Call this function to set the javascript validation handler for all valdiation on your site. This could be called from _config.php to set site-wide javascript validation, or from ContentController::init() to affect only the front-end site. Use instance method `Validator::setJavascriptValidationHandler()` to only set handler for a specific form instance. #### Parameters $handler $handler A string representing the handler to use: 'prototype' or 'none'.
`public static string` | # `get_javascript_validator_handler( )` Returns global validation handler used for all forms by default, unless overwritten by `Validator::setJavascriptValidationHandler()`. Returns global validation handler used for all forms by default, unless overwritten by `Validator::setJavascriptValidationHandler()`. #### Returns `string` string
`public` | # `setJavascriptValidationHandler( string $handler )` Set JavaScript validation for this validator. Use static method `Validator::set_javascript_validation_handler()` to set handlers globally. Set JavaScript validation for this validator. Use static method `Validator::set_javascript_validation_handler()` to set handlers globally. #### Parameters $handler string $handler
`public string` | # `getJavascriptValidationHandler( )` Gets the current javascript validation handler for this form. If not set, falls back to the global static `Validator::$javascript_validation_handler`. Gets the current javascript validation handler for this form. If not set, falls back to the global static `Validator::$javascript_validation_handler`. #### Returns `string` string
`public` | # `setForm( `Form` $form )` #### Parameters $form Form $form
`public array` | # `validate( )` #### Returns `array` Errors (if any)
`public` | # `validationError( mixed $fieldName, mixed $message, mixed $messageType = '' )` Callback to register an error on a field (Called from implementations of `FormField::validate()`) Callback to register an error on a field (Called from implementations of `FormField::validate()`) #### Parameters $fieldName $fieldName name of the field $message $message error message to display $messageType $messageType optional parameter, gets loaded into the HTML class attribute in the rendered output. See `Validator::getErrors()` for details.
`public` | # `showError( )` #### Deprecated 2.4 Use Validator->getErrors() and custom code
`public` | # `getCombinedError( )` #### Deprecated 2.4 Use custom code
`public` | # `getError( )` #### Deprecated 2.4 Use getErrors()
`public array` | # `getErrors( )` Returns all errors found by a previous call to `Validator::validate()`. The array contains the following keys for each error: 'fieldName': the name of the FormField instance 'message': Validation message (optionally localized) 'messageType': Arbitrary type of the message which is rendered as a CSS class in the FormField template, e.g. <span class="message (type)">. Usually "bad|message|validation|required", which renders differently if sapphire/css/Form.css is included. Returns all errors found by a previous call to `Validator::validate()`. The array contains the following keys for each error: 'fieldName': the name of the FormField instance 'message': Validation message (optionally localized) 'messageType': Arbitrary type of the message which is rendered as a CSS class in the FormField template, e.g. <span class="message (type)">. Usually "bad|message|validation|required", which renders differently if sapphire/css/Form.css is included. #### Returns `array` array
`public` | # `requireField( mixed $fieldName, mixed $data )`
`public` | # `includeJavascriptValidation( )`
`public` | # `fieldIsRequired( mixed $fieldName )` Returns true if the named field is "required". Used by FormField to return a value for FormField::Required(), to do things like show *s on the form template. By default, it always returns false. Returns true if the named field is "required". Used by FormField to return a value for FormField::Required(), to do things like show *s on the form template. By default, it always returns false. #### Used by ``FormField::Required()``
`abstract public` | # `javascript( )`
`abstract public` | # `php( mixed $data )`

### Methods inherited from Object

`__call()`, `__construct()`, `__toString()`, `__wakeup()`, `addMethodsFrom()`, `addStaticVars()`, `addWrapperMethod()`, `add_extension()`, `add_static_var()`, `allMethodNames()`, `cacheToFile()`, `cacheToFileWithArgs()`, `clearCache()`, `combined_static()`, `create()`, `createMethod()`, `create_from_string()`, `defineMethods()`, `exists()`, `extInstance()`, `extend()`, `getCustomClass()`, `getExtensionInstance()`, `getExtensionInstances()`, `get_extensions()`, `get_static()`, `hasExtension()`, `hasMethod()`, `has_extension()`, `invokeWithExtensions()`, `is_a()`, `loadCache()`, `parentClass()`, `parse_class_spec()`, `remove_extension()`, `sanitiseCachename()`, `saveCache()`, `set_stat()`, `set_static()`, `set_uninherited()`, `stat()`, `strong_create()`, `uninherited()`, `uninherited_static()`, `useCustomClass()`

### Magic methods summary

## Properties summary

`protected `Form`` | $form | # $form $form
`protected array` | $errors | # $errors $errors
`protected static string` | $javascript_validation_handler | # Static for default value of $this->javascriptValidationHandler. Set with Validator::set_javascript_validation_handler(); Static for default value of $this->javascriptValidationHandler. Set with Validator::set_javascript_validation_handler();
`protected string` | $javascriptValidationHandler | # Handler for javascript validation. Can be "prototype" or "none". Handler for javascript validation. Can be "prototype" or "none".

### Properties inherited from Object

`$class`, `$extension_instances`, `$extensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -