silverstripe-framework/forms/Validator.php

124 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2015-04-27 01:33:13 +02:00
/**
* This validation class handles all form and custom form validation through
* the use of Required fields.
2015-04-27 01:33:13 +02:00
*
* Relies on javascript for client-side validation, and marking fields after serverside validation.
2015-04-27 01:33:13 +02:00
*
* Acts as a visitor to individual form fields.
2015-04-27 01:33:13 +02:00
*
* @package forms
* @subpackage validators
2015-04-27 01:33:13 +02:00
*/
abstract class Validator extends Object {
/**
* @var Form $form
*/
protected $form;
2015-04-27 01:33:13 +02:00
/**
* @var array $errors
*/
protected $errors;
/**
* @param Form $form
2015-04-27 01:33:13 +02:00
*
* @return static
*/
public function setForm($form) {
$this->form = $form;
2015-04-27 01:33:13 +02:00
return $this;
}
2015-04-27 01:33:13 +02:00
/**
2015-04-27 01:33:13 +02:00
* Returns any errors there may be.
*
* @return null|array
*/
2015-04-27 01:33:13 +02:00
public function validate() {
$this->errors = null;
2015-04-27 01:33:13 +02:00
$this->php($this->form->getData());
2015-04-27 01:33:13 +02:00
return $this->errors;
}
2015-04-27 01:33:13 +02:00
/**
* Callback to register an error on a field (Called from implementations of {@link FormField::validate})
2015-04-27 01:33:13 +02:00
*
* @param string $fieldName name of the field
* @param string $message error message to display
* @param string $messageType optional parameter, gets loaded into the HTML class attribute in the rendered output.
*
* See {@link getErrors()} for details.
*/
2015-04-27 01:33:13 +02:00
public function validationError($fieldName, $message, $messageType = '') {
$this->errors[] = array(
'fieldName' => $fieldName,
'message' => $message,
'messageType' => $messageType,
);
}
2015-04-27 01:33:13 +02:00
/**
* Returns all errors found by a previous call to {@link validate()}.
2015-04-27 01:33:13 +02:00
*
* 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 framework/css/Form.css is included.
2015-04-27 01:33:13 +02:00
*
* @return array
*/
public function getErrors() {
return $this->errors;
}
2015-04-27 01:33:13 +02:00
/**
* @param string $fieldName
* @param array $data
*/
public function requireField($fieldName, $data) {
if(is_array($data[$fieldName]) && count($data[$fieldName])) {
2015-04-27 01:33:13 +02:00
foreach($data[$fieldName] as $componentKey => $componentValue) {
if(!strlen($componentValue)) {
$this->validationError(
$fieldName,
sprintf('%s %s is required', $fieldName, $componentKey),
'required'
);
}
}
} else if(!strlen($data[$fieldName])) {
2015-04-27 01:33:13 +02:00
$this->validationError(
$fieldName,
sprintf('%s is required', $fieldName),
'required'
);
}
}
2015-04-27 01:33:13 +02:00
/**
* Returns true if the named field is "required".
2015-04-27 01:33:13 +02:00
*
* Used by FormField to return a value for FormField::Required(), to do things like show *s on the form template.
2015-04-27 01:33:13 +02:00
*
* By default, it always returns false.
*/
public function fieldIsRequired($fieldName) {
return false;
}
2015-04-27 01:33:13 +02:00
/**
* @param array $data
*
* @return mixed
*/
abstract public function php($data);
}