2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2007-09-16 18:55:47 +02:00
|
|
|
* Required Fields allows you to set which fields
|
2007-07-19 12:40:28 +02:00
|
|
|
* need to be present before submitting the form
|
2007-09-16 18:55:47 +02:00
|
|
|
* Submit an array of arguments or each field as a
|
|
|
|
* seperate argument. Validation is performed on a name by
|
2007-07-19 12:40:28 +02:00
|
|
|
* name basis.
|
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
|
|
|
/**
|
2007-09-16 18:55:47 +02:00
|
|
|
* Pass each field to be validated as a seperate argument
|
2007-07-19 12:40:28 +02:00
|
|
|
* to the constructor of this object. (an array of elements are ok)
|
|
|
|
*/
|
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.
|
|
|
|
*/
|
|
|
|
public function removeValidation(){
|
2011-04-29 05:12:21 +02:00
|
|
|
$this->required = array();
|
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() {
|
2012-12-08 12:20:20 +01: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
|
|
|
/**
|
|
|
|
* Allows validation of fields via specification of a php function for validation which is executed after
|
|
|
|
* the form is submitted
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function php($data) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$valid = true;
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$fields = $this->form->Fields();
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$valid = ($field->validate($this) && $valid);
|
|
|
|
}
|
|
|
|
if($this->required) {
|
2010-12-05 09:34:42 +01:00
|
|
|
foreach($this->required as $fieldName) {
|
2012-05-29 04:37:26 +02:00
|
|
|
if(!$fieldName) continue;
|
|
|
|
|
2010-10-19 02:43:38 +02:00
|
|
|
$formField = $fields->dataFieldByName($fieldName);
|
2010-10-19 02:51:18 +02:00
|
|
|
|
|
|
|
$error = true;
|
|
|
|
// 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;
|
|
|
|
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(
|
|
|
|
'Form.FIELDISREQUIRED',
|
|
|
|
'{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;
|
|
|
|
}
|
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"
|
|
|
|
);
|
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
|
|
|
/**
|
|
|
|
* Add's a single required field to requiredfields stack
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addRequiredField( $field ) {
|
2013-07-15 22:44:44 +02:00
|
|
|
$this->required[$field] = $field;
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeRequiredField($field) {
|
2013-07-15 22:44:44 +02:00
|
|
|
unset($this->required[$field]);
|
|
|
|
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
|
|
|
/**
|
|
|
|
* allows you too add more required fields to this object after construction.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function appendRequiredFields($requiredFields){
|
2013-07-15 22:44:44 +02:00
|
|
|
$this->required = $this->required + ArrayLib::valuekey($requiredFields->getRequired());
|
|
|
|
return $this;
|
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.
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* getter function for append
|
|
|
|
*/
|
2012-09-19 12:07:39 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|