mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #4109 from assertchris/clean-up-validator
Clean up Validator
This commit is contained in:
commit
d0e40c3c31
@ -1,22 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This validation class handles all form and custom form validation through
|
* This validation class handles all form and custom form validation through
|
||||||
* the use of Required fields.
|
* the use of Required fields.
|
||||||
*
|
*
|
||||||
* Relies on javascript for client-side validation, and marking fields after serverside validation.
|
* Relies on javascript for client-side validation, and marking fields after serverside validation.
|
||||||
*
|
*
|
||||||
* Acts as a visitor to individual form fields.
|
* Acts as a visitor to individual form fields.
|
||||||
*
|
*
|
||||||
* @package forms
|
* @package forms
|
||||||
* @subpackage validators
|
* @subpackage validators
|
||||||
*/
|
*/
|
||||||
abstract class Validator extends Object {
|
abstract class Validator extends Object {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Form $form
|
* @var Form $form
|
||||||
*/
|
*/
|
||||||
protected $form;
|
protected $form;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array $errors
|
* @var array $errors
|
||||||
*/
|
*/
|
||||||
@ -24,74 +24,100 @@ abstract class Validator extends Object {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Form $form
|
* @param Form $form
|
||||||
|
*
|
||||||
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setForm($form) {
|
public function setForm($form) {
|
||||||
$this->form = $form;
|
$this->form = $form;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array Errors (if any)
|
* Returns any errors there may be.
|
||||||
|
*
|
||||||
|
* @return null|array
|
||||||
*/
|
*/
|
||||||
public function validate(){
|
public function validate() {
|
||||||
$this->errors = null;
|
$this->errors = null;
|
||||||
|
|
||||||
$this->php($this->form->getData());
|
$this->php($this->form->getData());
|
||||||
|
|
||||||
return $this->errors;
|
return $this->errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback to register an error on a field (Called from implementations of {@link FormField::validate})
|
* Callback to register an error on a field (Called from implementations of {@link FormField::validate})
|
||||||
*
|
*
|
||||||
* @param $fieldName name of the field
|
* @param string $fieldName name of the field
|
||||||
* @param $message error message to display
|
* @param string $message error message to display
|
||||||
* @param $messageType optional parameter, gets loaded into the HTML class attribute in the rendered output.
|
* @param string $messageType optional parameter, gets loaded into the HTML class attribute in the rendered output.
|
||||||
* See {@link getErrors()} for details.
|
*
|
||||||
|
* See {@link getErrors()} for details.
|
||||||
*/
|
*/
|
||||||
public function validationError($fieldName, $message, $messageType='') {
|
public function validationError($fieldName, $message, $messageType = '') {
|
||||||
$this->errors[] = array(
|
$this->errors[] = array(
|
||||||
'fieldName' => $fieldName,
|
'fieldName' => $fieldName,
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
'messageType' => $messageType,
|
'messageType' => $messageType,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all errors found by a previous call to {@link validate()}.
|
* Returns all errors found by a previous call to {@link validate()}.
|
||||||
|
*
|
||||||
* The array contains the following keys for each error:
|
* The array contains the following keys for each error:
|
||||||
* - 'fieldName': the name of the FormField instance
|
* - 'fieldName': the name of the FormField instance
|
||||||
* - 'message': Validation message (optionally localized)
|
* - 'message': Validation message (optionally localized)
|
||||||
* - 'messageType': Arbitrary type of the message which is rendered as a CSS class in the FormField template,
|
* - '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
|
* e.g. <span class="message (type)">. Usually "bad|message|validation|required", which renders differently
|
||||||
* if framework/css/Form.css is included.
|
* if framework/css/Form.css is included.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getErrors() {
|
public function getErrors() {
|
||||||
return $this->errors;
|
return $this->errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $fieldName
|
||||||
|
* @param array $data
|
||||||
|
*/
|
||||||
public function requireField($fieldName, $data) {
|
public function requireField($fieldName, $data) {
|
||||||
if(is_array($data[$fieldName]) && count($data[$fieldName])) {
|
if(is_array($data[$fieldName]) && count($data[$fieldName])) {
|
||||||
foreach($data[$fieldName] as $componentkey => $componentVal){
|
foreach($data[$fieldName] as $componentKey => $componentValue) {
|
||||||
if(!strlen($componentVal)) {
|
if(!strlen($componentValue)) {
|
||||||
$this->validationError($fieldName, "$fieldName $componentkey is required", "required");
|
$this->validationError(
|
||||||
|
$fieldName,
|
||||||
|
sprintf('%s %s is required', $fieldName, $componentKey),
|
||||||
|
'required'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if(!strlen($data[$fieldName])) {
|
} else if(!strlen($data[$fieldName])) {
|
||||||
$this->validationError($fieldName, "$fieldName is required", "required");
|
$this->validationError(
|
||||||
|
$fieldName,
|
||||||
|
sprintf('%s is required', $fieldName),
|
||||||
|
'required'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the named field is "required".
|
* Returns true if the named field is "required".
|
||||||
|
*
|
||||||
* Used by FormField to return a value for FormField::Required(), to do things like show *s on the form template.
|
* Used by FormField to return a value for FormField::Required(), to do things like show *s on the form template.
|
||||||
|
*
|
||||||
* By default, it always returns false.
|
* By default, it always returns false.
|
||||||
*/
|
*/
|
||||||
public function fieldIsRequired($fieldName) {
|
public function fieldIsRequired($fieldName) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
abstract public function php($data);
|
abstract public function php($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user