2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This validation class handles all form and custom form validation through
|
|
|
|
* the use of Required fields.
|
2008-03-06 03:06:47 +01:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* Relies on javascript for client-side validation, and marking fields after serverside validation.
|
|
|
|
*
|
2008-03-06 03:06:47 +01:00
|
|
|
* Acts as a visitor to individual form fields.
|
|
|
|
*
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Automatically mark fields after serverside validation and replace the form through
|
2007-07-19 12:40:28 +02:00
|
|
|
* FormResponse if the request was made by ajax.
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage validators
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
abstract class Validator extends Object {
|
2009-03-03 03:24:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Form $form
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $form;
|
2009-03-03 03:24:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $errors
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $errors;
|
2012-03-09 00:12:33 +01:00
|
|
|
|
2008-08-18 02:07:37 +02:00
|
|
|
/**
|
2012-03-09 00:12:33 +01:00
|
|
|
* @deprecated 3.0 Use custom javascript validation instead
|
2008-08-18 02:07:37 +02:00
|
|
|
*/
|
2009-03-03 03:24:29 +01:00
|
|
|
public static function set_javascript_validation_handler($handler) {
|
2012-03-09 00:12:33 +01:00
|
|
|
Deprecation::notice('3.0', 'Use custom javascript validation instead.');
|
2009-03-03 03:24:29 +01:00
|
|
|
}
|
2012-03-09 00:12:33 +01:00
|
|
|
|
2009-03-03 03:24:29 +01:00
|
|
|
/**
|
2012-03-09 00:12:33 +01:00
|
|
|
* @deprecated 3.0 Use custom javascript validation instead
|
2009-03-03 03:24:29 +01:00
|
|
|
*/
|
|
|
|
public static function get_javascript_validator_handler() {
|
2012-03-09 00:12:33 +01:00
|
|
|
Deprecation::notice('3.0', 'Use custom javascript validation instead.');
|
2008-08-18 02:07:37 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-18 02:07:37 +02:00
|
|
|
/**
|
2012-03-09 00:12:33 +01:00
|
|
|
* @deprecated 3.0 Use custom javascript validation instead
|
2008-08-18 02:07:37 +02:00
|
|
|
*/
|
|
|
|
public function setJavascriptValidationHandler($handler) {
|
2012-03-09 00:12:33 +01:00
|
|
|
Deprecation::notice('3.0', 'Use custom javascript validation instead.');
|
2008-08-18 02:07:37 +02:00
|
|
|
}
|
2009-03-03 03:24:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current javascript validation handler for this form.
|
|
|
|
* If not set, falls back to the global static {@link self::$javascript_validation_handler}.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getJavascriptValidationHandler() {
|
2012-03-09 00:12:33 +01:00
|
|
|
Deprecation::notice('3.0', 'Use custom javascript validation instead.');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-03-09 00:12:33 +01:00
|
|
|
|
2009-03-03 03:24:29 +01:00
|
|
|
/**
|
|
|
|
* @param Form $form
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function setForm($form) {
|
|
|
|
$this->form = $form;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-03-03 03:24:29 +01:00
|
|
|
/**
|
|
|
|
* @return array Errors (if any)
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function validate(){
|
|
|
|
$this->errors = null;
|
|
|
|
$this->php($this->form->getData());
|
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
2008-03-06 03:06:47 +01:00
|
|
|
/**
|
|
|
|
* Callback to register an error on a field (Called from implementations of {@link FormField::validate})
|
|
|
|
*
|
|
|
|
* @param $fieldName name of the field
|
|
|
|
* @param $message error message to display
|
2009-03-12 17:47:29 +01:00
|
|
|
* @param $messageType optional parameter, gets loaded into the HTML class attribute in the rendered output. See {@link getErrors()} for details.
|
2008-03-06 03:06:47 +01:00
|
|
|
*/
|
2009-03-12 17:47:29 +01:00
|
|
|
function validationError($fieldName, $message, $messageType='') {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->errors[] = array(
|
|
|
|
'fieldName' => $fieldName,
|
|
|
|
'message' => $message,
|
|
|
|
'messageType' => $messageType,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-03-12 17:47:29 +01:00
|
|
|
/**
|
|
|
|
* Returns all errors found by a previous call to {@link validate()}.
|
|
|
|
* The array contains the following keys for each error:
|
|
|
|
* - 'fieldName': the name of the FormField instance
|
|
|
|
* - 'message': Validation message (optionally localized)
|
|
|
|
* - '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
|
|
|
|
* if sapphire/css/Form.css is included.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getErrors() {
|
2007-08-23 07:47:54 +02:00
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function requireField($fieldName, $data) {
|
2010-10-19 03:20:38 +02:00
|
|
|
if(is_array($data[$fieldName]) && count($data[$fieldName])) {
|
|
|
|
foreach($data[$fieldName] as $componentkey => $componentVal){
|
|
|
|
if(!strlen($componentVal)) $this->validationError($fieldName, "$fieldName $componentkey is required.", "required");
|
|
|
|
}
|
|
|
|
|
|
|
|
}else if(!strlen($data[$fieldName])) $this->validationError($fieldName, "$fieldName is required.", "required");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* By default, it always returns false.
|
|
|
|
*/
|
|
|
|
function fieldIsRequired($fieldName) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract function php($data);
|
|
|
|
}
|
2012-02-12 21:22:11 +01:00
|
|
|
|