ocs /lib/pkp/classes/form/validation/FormValidator.inc.php

Language PHP Lines 174
MD5 Hash 9a42ccc5a71d0592ed197ab037c6f6a9
Repository https://github.com/lib-uoguelph-ca/ocs.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php

/**
 * @defgroup form_validation
 */

/**
 * @file classes/form/validation/FormValidator.inc.php
 *
 * Copyright (c) 2000-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class FormValidator
 * @ingroup form_validation
 *
 * @brief Class to represent a form validation check.
 */

// The two allowed states for the type field
define('FORM_VALIDATOR_OPTIONAL_VALUE', 'optional');
define('FORM_VALIDATOR_REQUIRED_VALUE', 'required');

class FormValidator {

	/** @var Form The Form associated with the check */
	var $_form;

	/** @var string The name of the field */
	var $_field;

	/** @var string The type of check ("required" or "optional") */
	var $_type;

	/** @var string The error message associated with a validation failure */
	var $_message;

	/** @var Validator The validator used to validate the field */
	var $_validator;

	/**
	 * Constructor.
	 * @param $form Form the associated form
	 * @param $field string the name of the associated field
	 * @param $type string the type of check, either "required" or "optional"
	 * @param $message string the error message for validation failures (i18n key)
	 * @param $validator Validator the validator used to validate this form field (optional)
	 */
	function FormValidator(&$form, $field, $type, $message, $validator = null) {
		$this->_form =& $form;
		$this->_field = $field;
		$this->_type = $type;
		$this->_message = $message;
		$this->_validator =& $validator;

		$form->cssValidation[$field] = array();
		if ($type == FORM_VALIDATOR_REQUIRED_VALUE) {
			array_push($form->cssValidation[$field], 'required');
		}
	}


	//
	// Setters and Getters
	//
	/**
	 * Get the field associated with the check.
	 * @return string
	 */
	function getField() {
		return $this->_field;
	}

	/**
	 * Get the error message associated with a failed validation check.
	 * @return string
	 */
	function getMessage() {
		return __($this->_message);
	}

	/**
	 * Set the form associated with this check. Used only for PHP4
	 * compatibility when instantiating without =& (which is deprecated).
	 * SHOULD NOT BE USED otherwise.
	 * See http://pkp.sfu.ca/wiki/index.php/Information_for_Developers#Use_of_.24this_in_the_constructor
	 * for a full explanation.
	 */
	function setForm(&$form) {
		$this->_form =& $form;
	}

	/**
	 * Get the form associated with the check
	 * @return Form
	 */
	function &getForm() {
		return $this->_form;
	}

	/**
	 * Get the validator associated with the check
	 * @return Validator
	 */
	function &getValidator() {
		return $this->_validator;
	}

	/**
	 * Get the type of the validated field ('optional' or 'required')
	 * @return string
	 */
	function getType() {
		return $this->_type;
	}


	//
	// Public methods
	//
	/**
	 * Check if field value is valid.
	 * Default check is that field is either optional or not empty.
	 * @return boolean
	 */
	function isValid() {
		if ($this->isEmptyAndOptional()) return true;

		$validator =& $this->getValidator();
		if (is_null($validator)) {
			// Default check: field must not be empty.
			$fieldValue = $this->getFieldValue();
			if (is_scalar($fieldValue)) {
				return $fieldValue !== '';
			} else {
				return $fieldValue !== array();
			}
		} else {
			// Delegate to the validator for the field value check.
			return $validator->isValid($this->getFieldValue());
		}
	}

	//
	// Protected helper methods
	//
	/**
	 * Get field value
	 * @return mixed
	 */
	function getFieldValue() {
		$form =& $this->getForm();
		$fieldValue = $form->getData($this->getField());
		if (is_null($fieldValue) || is_scalar($fieldValue)) $fieldValue = trim((string)$fieldValue);
		return $fieldValue;
	}

	/**
	 * Check if field value is empty and optional.
	 * @return boolean
	 */
	function isEmptyAndOptional() {
		if ($this->getType() != FORM_VALIDATOR_OPTIONAL_VALUE) return false;

		$fieldValue = $this->getFieldValue();
		if (is_scalar($fieldValue)) {
			return $fieldValue == '';
		} else {
			return empty($fieldValue);
		}
	}
}

?>
Back to Top