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() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$Required = func_get_args();
|
2007-08-27 07:13:43 +02:00
|
|
|
if( isset($Required[0]) && is_array( $Required[0] ) )
|
2007-07-19 12:40:28 +02:00
|
|
|
$Required = $Required[0];
|
|
|
|
$this->required = $Required;
|
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) {
|
2007-09-16 18:55:47 +02:00
|
|
|
$this->useLabels = $flag;
|
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(){
|
|
|
|
$this->required = null;
|
|
|
|
}
|
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)) {
|
2010-12-05 09:34:42 +01:00
|
|
|
if ($formField instanceof FileField && isset($value['error']) && $value['error']) {
|
|
|
|
$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 ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->required[] = $field;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeRequiredField($field) {
|
2012-12-18 10:57:11 +01:00
|
|
|
foreach ($this->required as $i => $required) {
|
|
|
|
if ($field == $required) {
|
|
|
|
array_splice($this->required, $i);
|
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){
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->required = array_merge($this->required,$requiredFields->getRequired());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return in_array($fieldName, $this->required);
|
|
|
|
}
|
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(){
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->required;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|