/** * Method to validate a RokCommon_Form_AbstractField object based on field data. * * @param string $element The XML element object representation of the form field. * @param string $group The optional dot-separated form group path on which to find the field. * @param mixed $value The optional value to use as the default for the field. * @param object $input An optional RokCommon_Registry object with the entire data set to validate * against the entire form. * * @return mixed Boolean true if field value is valid, RokCommon_Form_Exception on failure. * * @since 11.1 */ protected function validateField($element, $group = null, $value = null, $input = null) { // Make sure there is a valid RokCommon_XMLElement. if (!$element instanceof RokCommon_XMLElement) { return new RokCommon_Form_Exception(rc__('JLIB_FORM_ERROR_VALIDATE_FIELD'), -1, E_ERROR); } // Initialise variables. $valid = true; // Check if the field is required. $required = (string) $element['required'] == 'true' || (string) $element['required'] == 'required'; if ($required) { // If the field is required and the value is empty return an error message. if ($value === '' || $value === null) { // Does the field have a defined error message? if ($element['message']) { $message = $element['message']; } else { if ($element['label']) { $message = rc__($element['label']); } else { $message = rc__($element['name']); } $message = rc_n('JLIB_FORM_VALIDATE_FIELD_REQUIRED', $message); } return new RokCommon_Form_Exception($message, 2, E_WARNING); } } // Get the field validation rule. if ($type = (string) $element['validate']) { // Load the RokCommon_Form_AbstractValidator object for the field. $rule = $this->loadRuleType($type); // If the object could not be loaded return an error message. if ($rule === false) { return new RokCommon_Form_Exception(rc_n('JLIB_FORM_VALIDATE_FIELD_RULE_MISSING', $type), -2, E_ERROR); } // Run the field validation rule test. $valid = $rule->test($element, $value, $group, $input, $this); // Check for an error in the validation test. if ($valid instanceof Exception) { return $valid; } } // Check if the field is valid. if ($valid === false) { // Does the field have a defined error message? $message = (string) $element['message']; if ($message) { return new RokCommon_Form_Exception(rc__($message), 1, E_WARNING); } else { return new RokCommon_Form_Exception(rc_n('JLIB_FORM_VALIDATE_FIELD_INVALID', rc__((string) $element['label'])), 1, E_WARNING); } } return true; }
/** * @param $string * @param $n */ function rc_ne($string, $n) { echo rc_n($string, $n); }