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 {
|
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
function __construct() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function debug() {
|
|
|
|
if(!is_array($this->required)) return false;
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$result = "<ul>";
|
|
|
|
foreach( $this->required as $name ){
|
|
|
|
$result .= "<li>$name</li>";
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$result .= "</ul>";
|
|
|
|
return $result;
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function javascript() {
|
|
|
|
$js = "";
|
|
|
|
$fields = $this->form->Fields();
|
2009-03-04 04:44:11 +01:00
|
|
|
$dataFields = $this->form->Fields()->dataFields();
|
|
|
|
if($dataFields) {
|
|
|
|
foreach($dataFields as $field) {
|
|
|
|
// if the field type has some special specific specification for validation of itself
|
|
|
|
$validationFunc = $field->jsValidation();
|
|
|
|
if($validationFunc) $js .= $validationFunc . "\n";
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
$useLabels = $this->useLabels ? 'true' : 'false';
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->required) {
|
|
|
|
foreach($this->required as $field) {
|
|
|
|
if($fields->dataFieldByName($field)) {
|
2007-08-23 07:47:54 +02:00
|
|
|
//$js .= "\t\t\t\t\trequire('$field', false, $useLabels);\n";
|
|
|
|
$js .= <<<JS
|
|
|
|
if(typeof fromAnOnBlur != 'undefined'){\n
|
|
|
|
if(fromAnOnBlur.name == '$field')\n
|
|
|
|
require(fromAnOnBlur);\n
|
|
|
|
}else{
|
|
|
|
require('$field');
|
|
|
|
}
|
|
|
|
JS;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $js;
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
function php($data) {
|
|
|
|
$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) {
|
2008-07-17 23:58:59 +02:00
|
|
|
foreach($this->required as $fieldName) {
|
|
|
|
$formField = $fields->dataFieldByName($fieldName);
|
|
|
|
if($formField && !$data[$fieldName]) {
|
2009-04-29 01:55:53 +02:00
|
|
|
$errorMessage = sprintf(_t('Form.FIELDISREQUIRED').'.', strip_tags('"' . ($formField->Title() ? $formField->Title() : $fieldName) . '"'));
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
function addRequiredField( $field ) {
|
|
|
|
$this->required[] = $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeRequiredField($field) {
|
|
|
|
for($i=0; $i<count($this->required); $i++) {
|
|
|
|
if($field == $this->required[$i]) {
|
|
|
|
unset($this->required[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
function appendRequiredFields($requiredFields){
|
|
|
|
$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.
|
|
|
|
*/
|
|
|
|
function fieldIsRequired($fieldName) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function getRequired(){
|
|
|
|
return $this->required;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:55:47 +02:00
|
|
|
?>
|