2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage validators
|
|
|
|
*/
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
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.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage validators
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class RequiredFields extends Validator{
|
|
|
|
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();
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->form->Fields()->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-09-16 18:55:47 +02:00
|
|
|
|
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) {
|
|
|
|
foreach($this->required as $field) {
|
|
|
|
if($fields->dataFieldByName($field) && !$data[$field]) {
|
2007-10-25 04:47:45 +02:00
|
|
|
$this->validationError(
|
|
|
|
$field,
|
|
|
|
sprintf(
|
|
|
|
_t('Form.FIELDISREQUIRED'),
|
|
|
|
strip_tags($field)
|
|
|
|
),
|
|
|
|
"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
|
|
|
?>
|