2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-04-27 01:33:13 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
2017-05-17 07:40:13 +02:00
|
|
|
use SilverStripe\Core\Config\Configurable;
|
|
|
|
use SilverStripe\Core\Extensible;
|
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
2016-11-23 06:09:10 +01:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-04-20 04:27:50 +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
|
|
|
*/
|
2017-05-17 07:40:13 +02:00
|
|
|
abstract class Validator
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
use Injectable;
|
|
|
|
use Configurable;
|
|
|
|
use Extensible;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
public function __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
|
|
|
/**
|
2016-11-23 06:09:10 +01:00
|
|
|
* @var ValidationResult $result
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2016-11-23 06:09:10 +01:00
|
|
|
protected $result;
|
2012-03-09 00:12:33 +01:00
|
|
|
|
2015-08-25 20:32:11 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $enabled = true;
|
|
|
|
|
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.
|
|
|
|
*
|
2016-11-23 06:09:10 +01:00
|
|
|
* @return ValidationResult
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->resetResult();
|
2015-08-25 20:32:11 +02:00
|
|
|
if ($this->getEnabled()) {
|
|
|
|
$this->php($this->form->getData());
|
|
|
|
}
|
2016-11-23 06:09:10 +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
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2016-11-23 06:09:10 +01:00
|
|
|
* @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
|
|
|
*/
|
2016-11-23 06:09:10 +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]',
|
2016-11-23 06:09:10 +01:00
|
|
|
* 'messageCast' => '[text|html]'
|
2016-11-29 00:31:16 +01:00
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @return null|array
|
|
|
|
*/
|
|
|
|
public function getErrors()
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
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
|
|
|
/**
|
2016-11-23 06:09:10 +01:00
|
|
|
* Get last validation result
|
|
|
|
*
|
|
|
|
* @return ValidationResult
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2016-11-23 06:09:10 +01:00
|
|
|
public function getResult()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2016-11-23 06:09:10 +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);
|
2016-11-23 06:09:10 +01:00
|
|
|
|
2015-08-25 20:32:11 +02:00
|
|
|
/**
|
|
|
|
* @param bool $enabled
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setEnabled($enabled)
|
|
|
|
{
|
|
|
|
$this->enabled = (bool)$enabled;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getEnabled()
|
|
|
|
{
|
|
|
|
return $this->enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function removeValidation()
|
|
|
|
{
|
|
|
|
$this->setEnabled(false);
|
|
|
|
$this->resetResult();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Clear current result
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
protected function resetResult()
|
|
|
|
{
|
|
|
|
$this->result = ValidationResult::create();
|
|
|
|
return $this;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|