silverstripe-framework/forms/Validator.php

127 lines
2.7 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. It relies on javascript for client-side validation, and marking fields after server-side
* validation. It acts as a visitor to individual form fields.
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage validators
2014-08-15 08:53:05 +02:00
*/
2017-11-30 18:49:46 +01:00
abstract class Validator extends SS_Object {
2014-08-15 08:53:05 +02:00
/**
* @var Form $form
*/
protected $form;
2014-08-15 08:53:05 +02:00
/**
* @var array $errors
*/
protected $errors;
/**
* @param Form $form
2015-04-27 01:33:13 +02:00
*
* @return $this
*/
public function setForm($form) {
$this->form = $form;
2015-04-27 01:33:13 +02:00
return $this;
}
2014-08-15 08:53:05 +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;
}
2014-08-15 08:53:05 +02:00
/**
* Callback to register an error on a field (Called from implementations of
* {@link FormField::validate}). The optional error message type parameter is loaded into the
* HTML class attribute.
2015-04-27 01:33:13 +02:00
*
* See {@link getErrors()} for details.
*
* @param string $fieldName
* @param string $errorMessage
* @param string $errorMessageType
*/
public function validationError($fieldName, $errorMessage, $errorMessageType = '') {
$this->errors[] = array(
'fieldName' => $fieldName,
'message' => $errorMessage,
'messageType' => $errorMessageType,
);
}
2014-08-15 08:53:05 +02:00
/**
* Returns all errors found by a previous call to {@link validate()}. The returned array has a
* structure resembling:
2015-04-27 01:33:13 +02:00
*
* <code>
* array(
* 'fieldName' => '[form field name]',
* 'message' => '[validation error message]',
* 'messageType' => '[bad|message|validation|required]',
* )
* </code>
2014-08-15 08:53:05 +02:00
*
* @return null|array
*/
public function getErrors() {
return $this->errors;
}
2014-08-15 08:53:05 +02:00
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'
);
}
}
2014-08-15 08:53:05 +02:00
/**
* Returns whether the field in question is required. This will usually display '*' next to the
* field. The base implementation always returns false.
2015-04-27 01:33:13 +02:00
*
* @param string $fieldName
2015-04-27 01:33:13 +02:00
*
* @return bool
*/
public function fieldIsRequired($fieldName) {
return false;
}
2014-08-15 08:53:05 +02:00
2015-04-27 01:33:13 +02:00
/**
* @param array $data
*
* @return mixed
*/
abstract public function php($data);
}