/** * Validates the values of the record against the database and any additional validation rules * * @throws fValidationException When the record, or one of the associated records, violates one of the validation rules for the class or can not be properly stored in the database * * @param boolean $return_messages If an array of validation messages should be returned instead of an exception being thrown * @param boolean $remove_column_names If column names should be removed from the returned messages, leaving just the message itself * @return void|array If $return_messages is TRUE, an array of validation messages will be returned */ public function validate($return_messages = FALSE, $remove_column_names = FALSE) { $class = get_class($this); if (fORM::getActiveRecordMethod($class, 'validate')) { return $this->__call('validate', array($return_messages)); } $validation_messages = array(); fORM::callHookCallbacks($this, 'pre::validate()', $this->values, $this->old_values, $this->related_records, $this->cache, $validation_messages); // Validate the local values $local_validation_messages = fORMValidation::validate($this, $this->values, $this->old_values); // Validate related records $related_validation_messages = fORMValidation::validateRelated($this, $this->values, $this->related_records); $validation_messages = array_merge($validation_messages, $local_validation_messages, $related_validation_messages); fORM::callHookCallbacks($this, 'post::validate()', $this->values, $this->old_values, $this->related_records, $this->cache, $validation_messages); $validation_messages = fORMValidation::replaceMessages($class, $validation_messages); $validation_messages = fORMValidation::reorderMessages($class, $validation_messages); if ($return_messages) { if ($remove_column_names) { $validation_messages = fValidationException::removeFieldNames($validation_messages); } return $validation_messages; } if (!empty($validation_messages)) { throw new fValidationException('The following problems were found:', $validation_messages); } }
/** * Checks for required fields, email field formatting and email header injection using values previously set * * @throws fValidationException When one of the options set for the object is violated * * @param boolean $return_messages If an array of validation messages should be returned instead of an exception being thrown * @param boolean $remove_field_names If field names should be removed from the returned messages, leaving just the message itself * @return void|array If $return_messages is TRUE, an array of validation messages will be returned */ public function validate($return_messages = FALSE, $remove_field_names = FALSE) { if (!$this->callback_rules && !$this->conditional_rules && !$this->date_fields && !$this->file_upload_rules && !$this->one_or_more_rules && !$this->only_one_rules && !$this->regex_rules && !$this->required_fields && !$this->valid_values_rules) { throw new fProgrammerException('No fields or rules have been added for validation'); } $messages = array(); $this->checkRequiredFields($messages); $this->checkFileUploadRules($messages); $this->checkConditionalRules($messages); $this->checkOneOrMoreRules($messages); $this->checkOnlyOneRules($messages); $this->checkValidValuesRules($messages); $this->checkDateFields($messages); $this->checkRegexRules($messages); $this->checkCallbackRules($messages); if ($this->regex_replacements) { $messages = preg_replace(array_keys($this->regex_replacements), array_values($this->regex_replacements), $messages); } if ($this->string_replacements) { $messages = str_replace(array_keys($this->string_replacements), array_values($this->string_replacements), $messages); } $messages = $this->reorderMessages($messages); if ($return_messages) { if ($remove_field_names) { $messages = fValidationException::removeFieldNames($messages); } return $messages; } if ($messages) { throw new fValidationException('The following problems were found:', $messages); } }