silverstripe-framework/src/Forms/Validator.php

135 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2015-04-27 01:33:13 +02:00
namespace SilverStripe\Forms;
use SilverStripe\Core\Object;
/**
* 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
*/
2016-11-29 00:31:16 +01:00
abstract class Validator extends Object
{
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* @var Form $form
*/
protected $form;
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* @var array $errors
*/
protected $errors;
2016-11-29 00:31:16 +01:00
/**
* @param Form $form
*
* @return $this
*/
public function setForm($form)
{
$this->form = $form;
2015-04-27 01:33:13 +02:00
2016-11-29 00:31:16 +01:00
return $this;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Returns any errors there may be.
*
* @return null|array
*/
public function validate()
{
$this->errors = null;
2015-04-27 01:33:13 +02:00
2016-11-29 00:31:16 +01:00
$this->php($this->form->getData());
2015-04-27 01:33:13 +02:00
2016-11-29 00:31:16 +01:00
return $this->errors;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01: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.
*
* 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
2016-11-29 00:31:16 +01:00
/**
* Returns all errors found by a previous call to {@link validate()}. The returned array has a
* structure resembling:
*
* <code>
* array(
* 'fieldName' => '[form field name]',
* 'message' => '[validation error message]',
* 'messageType' => '[bad|message|validation|required]',
* )
* </code>
*
* @return null|array
*/
public function getErrors()
{
return $this->errors;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* @param string $fieldName
* @param array $data
*/
public function requireField($fieldName, $data)
{
if (is_array($data[$fieldName]) && count($data[$fieldName])) {
foreach ($data[$fieldName] as $componentKey => $componentValue) {
if (!strlen($componentValue)) {
$this->validationError(
$fieldName,
sprintf('%s %s is required', $fieldName, $componentKey),
'required'
);
}
}
} elseif (!strlen($data[$fieldName])) {
$this->validationError(
$fieldName,
sprintf('%s is required', $fieldName),
'required'
);
}
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Returns whether the field in question is required. This will usually display '*' next to the
* field. The base implementation always returns false.
*
* @param string $fieldName
*
* @return bool
*/
public function fieldIsRequired($fieldName)
{
return false;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* @param array $data
*
* @return mixed
*/
abstract public function php($data);
}