Open Journal Systems  3.3.0
FormValidator.inc.php
1 <?php
19 // The two allowed states for the type field
20 define('FORM_VALIDATOR_OPTIONAL_VALUE', 'optional');
21 define('FORM_VALIDATOR_REQUIRED_VALUE', 'required');
22 
24 
26  var $_form;
27 
29  var $_field;
30 
32  var $_type;
33 
35  var $_message;
36 
38  var $_validator;
39 
48  function __construct(&$form, $field, $type, $message, $validator = null) {
49  $this->_form =& $form;
50  $this->_field = $field;
51  $this->_type = $type;
52  $this->_message = $message;
53  $this->_validator =& $validator;
54 
55  $form->cssValidation[$field] = array();
56  if ($type == FORM_VALIDATOR_REQUIRED_VALUE) {
57  array_push($form->cssValidation[$field], 'required');
58  }
59  }
60 
61 
62  //
63  // Setters and Getters
64  //
69  function getField() {
70  return $this->_field;
71  }
72 
77  function getMessage() {
78  return __($this->_message);
79  }
80 
85  function &getForm() {
86  return $this->_form;
87  }
88 
93  function &getValidator() {
94  return $this->_validator;
95  }
96 
101  function getType() {
102  return $this->_type;
103  }
104 
105 
106  //
107  // Public methods
108  //
114  function isValid() {
115  if ($this->isEmptyAndOptional()) return true;
116 
117  $validator =& $this->getValidator();
118  if (is_null($validator)) {
119  // Default check: field must not be empty.
120  $fieldValue = $this->getFieldValue();
121  if (is_scalar($fieldValue)) {
122  return $fieldValue !== '';
123  } else {
124  return $fieldValue !== array();
125  }
126  } else {
127  // Delegate to the validator for the field value check.
128  return $validator->isValid($this->getFieldValue());
129  }
130  }
131 
132  //
133  // Protected helper methods
134  //
139  function getFieldValue() {
140  $form =& $this->getForm();
141  $fieldValue = $form->getData($this->getField());
142  if (is_null($fieldValue) || is_scalar($fieldValue)) $fieldValue = trim((string)$fieldValue);
143  return $fieldValue;
144  }
145 
150  function isEmptyAndOptional() {
151  if ($this->getType() != FORM_VALIDATOR_OPTIONAL_VALUE) return false;
152 
153  $fieldValue = $this->getFieldValue();
154  if (is_scalar($fieldValue)) {
155  return $fieldValue == '';
156  } else {
157  return empty($fieldValue);
158  }
159  }
160 }
161 
162 
FormValidator\$_message
$_message
Definition: FormValidator.inc.php:47
FormValidator\__construct
__construct(&$form, $field, $type, $message, $validator=null)
Definition: FormValidator.inc.php:63
FormValidator\getField
getField()
Definition: FormValidator.inc.php:84
FormValidator\$_validator
$_validator
Definition: FormValidator.inc.php:53
FormValidator\isValid
isValid()
Definition: FormValidator.inc.php:129
FormValidator\getForm
& getForm()
Definition: FormValidator.inc.php:100
FormValidator\$_type
$_type
Definition: FormValidator.inc.php:41
FormValidator\getType
getType()
Definition: FormValidator.inc.php:116
FormValidator\getValidator
& getValidator()
Definition: FormValidator.inc.php:108
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
FormValidator\$_field
$_field
Definition: FormValidator.inc.php:35
FormValidator\getMessage
getMessage()
Definition: FormValidator.inc.php:92
FormValidator\isEmptyAndOptional
isEmptyAndOptional()
Definition: FormValidator.inc.php:165
FormValidator\getFieldValue
getFieldValue()
Definition: FormValidator.inc.php:154
FormValidator\$_form
$_form
Definition: FormValidator.inc.php:29