2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-04-27 01:33:13 +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
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage validators
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2017-11-30 18:49:46 +01:00
|
|
|
abstract class Validator extends SS_Object {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-03 03:24:29 +01:00
|
|
|
/**
|
|
|
|
* @var Form $form
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $form;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
|
2009-03-03 03:24:29 +01:00
|
|
|
/**
|
|
|
|
* @param Form $form
|
2015-04-27 01:33:13 +02:00
|
|
|
*
|
2016-04-20 04:27:50 +02:00
|
|
|
* @return $this
|
2009-03-03 03:24:29 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setForm($form) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->form = $form;
|
2015-04-27 01:33:13 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-03 03:24:29 +01:00
|
|
|
/**
|
2015-04-27 01:33:13 +02:00
|
|
|
* Returns any errors there may be.
|
|
|
|
*
|
|
|
|
* @return null|array
|
2009-03-03 03:24:29 +01:00
|
|
|
*/
|
2015-04-27 01:33:13 +02:00
|
|
|
public function validate() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->errors = null;
|
2015-04-27 01:33:13 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->php($this->form->getData());
|
2015-04-27 01:33:13 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->errors;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-06 03:06:47 +01:00
|
|
|
/**
|
2016-04-20 04:27:50 +02: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.
|
2015-04-27 01:33:13 +02:00
|
|
|
*
|
2014-11-12 03:19:12 +01:00
|
|
|
* See {@link getErrors()} for details.
|
2016-04-20 04:27:50 +02:00
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @param string $errorMessage
|
|
|
|
* @param string $errorMessageType
|
2008-03-06 03:06:47 +01:00
|
|
|
*/
|
2016-04-20 04:27:50 +02:00
|
|
|
public function validationError($fieldName, $errorMessage, $errorMessageType = '') {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->errors[] = array(
|
|
|
|
'fieldName' => $fieldName,
|
2016-04-20 04:27:50 +02:00
|
|
|
'message' => $errorMessage,
|
|
|
|
'messageType' => $errorMessageType,
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-12 17:47:29 +01:00
|
|
|
/**
|
2016-04-20 04:27:50 +02:00
|
|
|
* Returns all errors found by a previous call to {@link validate()}. The returned array has a
|
|
|
|
* structure resembling:
|
2015-04-27 01:33:13 +02:00
|
|
|
*
|
2016-04-20 04:27:50 +02:00
|
|
|
* <code>
|
|
|
|
* array(
|
|
|
|
* 'fieldName' => '[form field name]',
|
|
|
|
* 'message' => '[validation error message]',
|
|
|
|
* 'messageType' => '[bad|message|validation|required]',
|
|
|
|
* )
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-04-20 04:27:50 +02:00
|
|
|
* @return null|array
|
2009-03-12 17:47:29 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getErrors() {
|
2007-08-23 07:47:54 +02:00
|
|
|
return $this->errors;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-27 01:33:13 +02:00
|
|
|
/**
|
|
|
|
* @param string $fieldName
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField($fieldName, $data) {
|
2010-10-19 03:20:38 +02:00
|
|
|
if(is_array($data[$fieldName]) && count($data[$fieldName])) {
|
2015-04-27 01:33:13 +02:00
|
|
|
foreach($data[$fieldName] as $componentKey => $componentValue) {
|
|
|
|
if(!strlen($componentValue)) {
|
|
|
|
$this->validationError(
|
|
|
|
$fieldName,
|
|
|
|
sprintf('%s %s is required', $fieldName, $componentKey),
|
|
|
|
'required'
|
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2010-10-19 03:20:38 +02:00
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
} else if(!strlen($data[$fieldName])) {
|
2015-04-27 01:33:13 +02:00
|
|
|
$this->validationError(
|
|
|
|
$fieldName,
|
|
|
|
sprintf('%s is required', $fieldName),
|
|
|
|
'required'
|
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-04-20 04:27:50 +02:00
|
|
|
* Returns whether the field in question is required. This will usually display '*' next to the
|
|
|
|
* field. The base implementation always returns false.
|
2015-04-27 01:33:13 +02:00
|
|
|
*
|
2016-04-20 04:27:50 +02:00
|
|
|
* @param string $fieldName
|
2015-04-27 01:33:13 +02:00
|
|
|
*
|
2016-04-20 04:27:50 +02:00
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fieldIsRequired($fieldName) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-27 01:33:13 +02:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
abstract public function php($data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|