2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage validators
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify special required fields to be executed as part of form validation
|
|
|
|
* @package forms
|
|
|
|
* @subpackage validators
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class CustomRequiredFields extends RequiredFields{
|
|
|
|
protected $required;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass each field to be validated as a seperate argument
|
|
|
|
* __construct()'s arguments needs to be an array
|
|
|
|
* @mpeel Unfortunately the line 'function __construct(array $required)' breaks older versions of PHP 5, so remove the forcing of array
|
|
|
|
*/
|
|
|
|
function __construct($required) {
|
|
|
|
$this->required = $required;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the client side validation from form fields
|
|
|
|
* which is generated at the header of each page
|
|
|
|
*/
|
|
|
|
function javascript() {
|
2007-12-17 22:39:59 +01:00
|
|
|
$code = '';
|
2007-07-19 12:40:28 +02:00
|
|
|
$fields = $this->form->Fields();
|
|
|
|
foreach($fields as $field){
|
|
|
|
//if the field type has some special specific specification for validation of itself
|
|
|
|
$valid = $field->jsValidation();
|
|
|
|
if($valid){
|
|
|
|
$code .= $valid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_array($this->required)){
|
|
|
|
|
|
|
|
foreach($this->required as $field) {
|
2007-10-04 23:58:10 +02:00
|
|
|
if(is_array($field) && $field['js']){
|
|
|
|
$code .= $field['js'] . "\n";
|
|
|
|
}else if($fields->dataFieldByName($field)) {
|
|
|
|
$code .= " require('$field');\n";
|
|
|
|
//Tabs for output tabbing :-)
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
USER_ERROR("CustomRequiredFields::requiredfields is not set / not an array",E_USER_WARNING);
|
|
|
|
}
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the server side validation from form fields
|
|
|
|
* which is executed on form submission
|
|
|
|
*/
|
|
|
|
function php($data) {
|
|
|
|
$fields = $this->form->Fields();
|
|
|
|
$valid = true;
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$valid = ($field->validate($this) && $valid);
|
|
|
|
}
|
|
|
|
if($this->required){
|
|
|
|
foreach($this->required as $key => $field) {
|
2007-10-04 23:58:10 +02:00
|
|
|
if(is_array($field) && $field['php']){
|
|
|
|
eval($field['php']);
|
|
|
|
}else if($fields->dataFieldByName($field)) {
|
|
|
|
// if an error is found, the form is returned.
|
2007-11-19 03:05:56 +01:00
|
|
|
if(!$data[$field] || preg_match('/^\s*$/', $data[$field])) {
|
2007-10-25 04:47:45 +02:00
|
|
|
$this->validationError(
|
|
|
|
$field,
|
|
|
|
sprintf(_t('Form.FIELDISREQUIRED', "%s is required"), $field),
|
|
|
|
"required"
|
|
|
|
);
|
2007-10-04 23:58:10 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* allows you too add more required fields to this object after construction.
|
|
|
|
*/
|
|
|
|
function appendRequiredFields($requiredFields){
|
|
|
|
$this->required = array_merge($this->required,$requiredFields->getRequired());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-17 22:39:59 +01:00
|
|
|
?>
|