2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-10-16 00:29:43 +02:00
|
|
|
* Required Fields allows you to set which fields need to be present before
|
|
|
|
* submitting the form. Submit an array of arguments or each field as a separate
|
|
|
|
* argument.
|
|
|
|
*
|
|
|
|
* Validation is performed on a field by field basis through
|
|
|
|
* {@link FormField::validate}.
|
2009-04-29 01:55:53 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage validators
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-04-29 01:55:53 +02:00
|
|
|
class RequiredFields extends Validator {
|
2010-12-05 09:34:42 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $required;
|
|
|
|
protected $useLabels = true;
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-10-16 00:29:43 +02:00
|
|
|
* Pass each field to be validated as a seperate argument to the constructor
|
|
|
|
* of this object. (an array of elements are ok).
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct() {
|
2013-07-15 22:44:44 +02:00
|
|
|
$required = func_get_args();
|
|
|
|
if(isset($required[0]) && is_array($required[0])) {
|
|
|
|
$required = $required[0];
|
|
|
|
}
|
|
|
|
if(!empty($required)) {
|
|
|
|
$this->required = ArrayLib::valuekey($required);
|
|
|
|
} else {
|
|
|
|
$this->required = array();
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function useLabels($flag) {
|
2013-07-15 22:44:44 +02:00
|
|
|
Deprecation::notice('3.2', 'useLabels will be removed from 3.2, please do not use it or implement it yourself');
|
2007-09-16 18:55:47 +02:00
|
|
|
$this->useLabels = $flag;
|
2013-07-15 22:44:44 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Clears all the validation from this object.
|
2013-10-16 00:29:43 +02:00
|
|
|
*
|
|
|
|
* @return RequiredFields
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-10-16 00:29:43 +02:00
|
|
|
public function removeValidation() {
|
2011-04-29 05:12:21 +02:00
|
|
|
$this->required = array();
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2013-07-15 22:44:44 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Debug helper
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function debug() {
|
2013-10-16 00:29:43 +02:00
|
|
|
if(!is_array($this->required)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$result = "<ul>";
|
|
|
|
foreach( $this->required as $name ){
|
|
|
|
$result .= "<li>$name</li>";
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$result .= "</ul>";
|
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-10-16 00:29:43 +02:00
|
|
|
* Allows validation of fields via specification of a php function for
|
|
|
|
* validation which is executed after the form is submitted.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function php($data) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$valid = true;
|
|
|
|
$fields = $this->form->Fields();
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($fields as $field) {
|
|
|
|
$valid = ($field->validate($this) && $valid);
|
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->required) {
|
2010-12-05 09:34:42 +01:00
|
|
|
foreach($this->required as $fieldName) {
|
2013-10-16 00:29:43 +02:00
|
|
|
if(!$fieldName) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-29 04:37:26 +02:00
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
if($fieldName instanceof FormField) {
|
|
|
|
$formField = $fieldName;
|
|
|
|
$fieldName = $fieldName->getName();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$formField = $fields->dataFieldByName($fieldName);
|
|
|
|
}
|
2010-10-19 02:51:18 +02:00
|
|
|
|
|
|
|
$error = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:51:18 +02:00
|
|
|
// submitted data for file upload fields come back as an array
|
2010-10-19 03:31:30 +02:00
|
|
|
$value = isset($data[$fieldName]) ? $data[$fieldName] : null;
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2010-10-19 03:31:30 +02:00
|
|
|
if(is_array($value)) {
|
2013-07-15 22:44:44 +02:00
|
|
|
if($formField instanceof FileField && isset($value['error']) && $value['error']) {
|
2010-12-05 09:34:42 +01:00
|
|
|
$error = true;
|
2012-05-29 04:37:26 +02:00
|
|
|
} else {
|
2010-12-05 09:34:42 +01:00
|
|
|
$error = (count($value)) ? false : true;
|
|
|
|
}
|
2010-10-19 02:51:18 +02:00
|
|
|
} else {
|
|
|
|
// assume a string or integer
|
2010-10-19 03:31:30 +02:00
|
|
|
$error = (strlen($value)) ? false : true;
|
2010-10-19 02:51:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($formField && $error) {
|
2012-12-21 11:18:51 +01:00
|
|
|
$errorMessage = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Form.FIELDISREQUIRED',
|
2012-12-21 11:18:51 +01:00
|
|
|
'{name} is required',
|
|
|
|
array(
|
|
|
|
'name' => strip_tags(
|
|
|
|
'"' . ($formField->Title() ? $formField->Title() : $fieldName) . '"'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2009-04-29 01:55:53 +02:00
|
|
|
if($msg = $formField->getCustomValidationMessage()) {
|
|
|
|
$errorMessage = $msg;
|
|
|
|
}
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2007-10-25 04:47:45 +02:00
|
|
|
$this->validationError(
|
2008-07-17 23:58:59 +02:00
|
|
|
$fieldName,
|
2009-04-29 01:55:53 +02:00
|
|
|
$errorMessage,
|
2007-10-25 04:47:45 +02:00
|
|
|
"required"
|
|
|
|
);
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $valid;
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-10-16 00:29:43 +02:00
|
|
|
* Adds a single required field to required fields stack.
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* @return RequiredFields
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-10-16 00:29:43 +02:00
|
|
|
public function addRequiredField($field) {
|
2013-07-15 22:44:44 +02:00
|
|
|
$this->required[$field] = $field;
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2013-07-15 22:44:44 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2013-10-16 00:29:43 +02:00
|
|
|
/**
|
|
|
|
* Removes a required field
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* @return RequiredFields
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeRequiredField($field) {
|
2013-07-15 22:44:44 +02:00
|
|
|
unset($this->required[$field]);
|
2013-10-16 00:29:43 +02:00
|
|
|
|
2013-07-15 22:44:44 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-10-16 00:29:43 +02:00
|
|
|
* Add {@link RequiredField} objects together
|
|
|
|
*
|
|
|
|
* @param RequiredFields
|
|
|
|
*
|
|
|
|
* @return RequiredFields
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-10-16 00:29:43 +02:00
|
|
|
public function appendRequiredFields($requiredFields) {
|
|
|
|
$this->required = $this->required + ArrayLib::valuekey(
|
|
|
|
$requiredFields->getRequired()
|
|
|
|
);
|
|
|
|
|
2013-07-15 22:44:44 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the named field is "required".
|
2013-10-16 00:29:43 +02:00
|
|
|
*
|
|
|
|
* Used by {@link FormField} to return a value for FormField::Required(),
|
|
|
|
* to do things like show *s on the form template.
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
*
|
|
|
|
* @return boolean
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fieldIsRequired($fieldName) {
|
2013-07-15 22:44:44 +02:00
|
|
|
return isset($this->required[$fieldName]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-10-16 00:29:43 +02:00
|
|
|
* Return the required fields
|
|
|
|
*
|
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-10-16 00:29:43 +02:00
|
|
|
public function getRequired() {
|
2013-07-15 22:44:44 +02:00
|
|
|
return array_values($this->required);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|