_form = & $form; $this->_field = $field; $this->_type = $type; $this->_message = $message; $this->_validator = & $validator; $form->cssValidation[$field] = []; if ($type == self::FORM_VALIDATOR_REQUIRED_VALUE) { array_push($form->cssValidation[$field], 'required'); } } // // Setters and Getters // /** * Get the field associated with the check. * * @return string */ public function getField() { return $this->_field; } /** * Get the error message associated with a failed validation check. * * @return string */ public function getMessage() { return __($this->_message); } /** * Get the form associated with the check * * @return Form */ public function &getForm() { return $this->_form; } /** * Get the validator associated with the check * * @return Validator */ public function &getValidator() { return $this->_validator; } /** * Get the type of the validated field ('optional' or 'required') * * @return string */ public 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 bool */ public 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 !== []; } } else { // Delegate to the validator for the field value check. return $validator->isValid($this->getFieldValue()); } } // // Protected helper methods // /** * Get field value */ public 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 bool */ public function isEmptyAndOptional() { if ($this->getType() != self::FORM_VALIDATOR_OPTIONAL_VALUE) { return false; } $fieldValue = $this->getFieldValue(); if (is_scalar($fieldValue)) { return $fieldValue == ''; } else { return empty($fieldValue); } } } if (!PKP_STRICT_MODE) { class_alias('\PKP\form\validation\FormValidator', '\FormValidator'); define('FORM_VALIDATOR_OPTIONAL_VALUE', FormValidator::FORM_VALIDATOR_OPTIONAL_VALUE); define('FORM_VALIDATOR_REQUIRED_VALUE', FormValidator::FORM_VALIDATOR_REQUIRED_VALUE); }