silverstripe-framework/src/Forms/Validator.php

143 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2015-04-27 01:33:13 +02:00
namespace SilverStripe\Forms;
use SilverStripe\Core\Object;
use SilverStripe\ORM\ValidationResult;
/**
* 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
public function __construct()
{
parent::__construct();
$this->resetResult();
}
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 ValidationResult $result
2016-11-29 00:31:16 +01:00
*/
protected $result;
2016-11-29 00:31:16 +01:00
/**
* @param Form $form
* @return $this
*/
public function setForm($form)
{
$this->form = $form;
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 ValidationResult
2016-11-29 00:31:16 +01:00
*/
public function validate()
{
$this->resetResult();
2016-11-29 00:31:16 +01:00
$this->php($this->form->getData());
return $this->result;
2016-11-29 00:31:16 +01:00
}
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 Field name for this error
* @param string $message The message string
* @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS
* class to the form, so other values can be used if desired.
* @param string|bool $cast Cast type; One of the CAST_ constant definitions.
* Bool values will be treated as plain text flag.
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function validationError(
$fieldName,
$message,
$messageType = ValidationResult::TYPE_ERROR,
$cast = ValidationResult::CAST_TEXT
) {
$this->result->addFieldError($fieldName, $message, $messageType, null, $cast);
return $this;
2016-11-29 00:31:16 +01:00
}
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]',
* 'messageCast' => '[text|html]'
2016-11-29 00:31:16 +01:00
* )
* </code>
*
* @return null|array
*/
public function getErrors()
{
if ($this->result) {
return $this->result->getMessages();
}
return null;
2016-11-29 00:31:16 +01:00
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Get last validation result
*
* @return ValidationResult
2016-11-29 00:31:16 +01:00
*/
public function getResult()
2016-11-29 00:31:16 +01:00
{
return $this->result;
2016-11-29 00:31:16 +01:00
}
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);
/**
* Clear current result
*
* @return $this
*/
protected function resetResult()
{
$this->result = ValidationResult::create();
return $this;
}
}