rules = array('required', 'numeric', 'boolean'); $config = require __DIR__.'/../config/config.php'; self::$instance->errorMsgs = $config['listMessagesValidationRules']; // rule loop and rule data retrieval foreach ($validationOptions as $fieldName => $rules) { $rulesArray = explode('|', $rules); $rulesArrayFiltered = array(); // getting field rules and other data foreach ($rulesArray as $key => $itemRule) { $rule = self::$instance->getRule($itemRule); $attributes = self::$instance->getAttributes($itemRule); $rulesArrayFiltered[$key] = array( 'rule' => $rule, ); // if the field has attributes, then add data $rulesArrayFiltered[$key]['attributes'] = !empty($attributes) ? $attributes : array(); } // start checking the field according to the rules self::$instance->validateField($fieldName, $rulesArrayFiltered); // Check if we should stop validation. if (self::$instance->shouldBail) { break; } } // If the request data is incorrect and if $sendErrorResponse is true - send error data. if ($sendErrorResponse && !self::isValid()) { // If AJAX return json with errors data if (defined('DOING_AJAX') && DOING_AJAX) { a4bJsonResponse(array('error' => ValidatorPostData::getErrors())); } else { // Redirect back and show errors as notices setcookie('old_post', base64_encode(json_encode(PostData::$postData)), time() + 2, '/'); a4bRedirectBackWithErrorNotices(ValidatorPostData::getErrors()); } } } /** * method of getting the name of the rule (cutting off extra data). * * @param string $rule */ public function getRule($rule) { $tpmArr = explode(':', $rule); return $tpmArr[0]; } /** * attribute retrieval method for rule. * * @param string $rule */ public function getAttributes($rule) { $tpmArr = explode(':', $rule); $attributes = array(); // if attributes are specified for the field, break them into an array if (isset($tpmArr[1])) { $attributes = explode(',', $tpmArr[1]); } return $attributes; } /** * field data validity method. * * @return bool */ public static function isValid() { return (count(self::$instance->errors) > 0) ? false : true; } /** * the method returns field validation errors. * * @return array */ public static function getErrors() { return self::$instance->errors; } /** * start checking the field according to the rules. * * @param string $fieldName * @param array $rulesArray */ private function validateField($fieldName, $rulesArray) { // iteration of the rules specified for the field foreach ($rulesArray as $itemRuleData) { $methodName = "rule{$itemRuleData['rule']}"; // checking the existence of a method for validating by rule name if (method_exists(self::$instance, $methodName)) { // call validation method $result = call_user_func_array(array(self::$instance, $methodName), array($fieldName, $itemRuleData)); } // Check if we should stop validation if (false === $result) { break; } } } /** * validation method according to the 'required' rule. * * @param string $fieldName * @param array $ruleData */ private function ruleRequired($fieldName, $ruleData) { $attributes = $ruleData['attributes']; // If field required only if empty another field ($attributes[1]) if (isset($attributes[0]) && 'if_empty' === $attributes[0] && isset($attributes[1])) { // If condition field is empty, write error. Else validation is passed. if ( empty(PostData::$postData[$attributes[1]]) // empty conditioned field && ( !isset(PostData::$postData[$fieldName]) // empty required field || '' === PostData::$postData[$fieldName] ) ) { $this->_error($this->errorMsgs['required'], $fieldName); } } elseif ( !isset(PostData::$postData[$fieldName]) || '' === PostData::$postData[$fieldName] ) { // if there is no field or the field is empty, write error $this->_error($this->errorMsgs['required'], $fieldName); } } /** * Validation method according to the 'requiredItem' rule. * * @param string $fieldName * @param array $ruleData */ private function ruleRequiredItem($fieldName, $ruleData) { $attributes = $ruleData['attributes']; // If field required only if empty another field ($attributes[1]) if (isset($attributes[0]) && 'if_empty' === $attributes[0] && isset($attributes[1])) { // If condition field is empty, write error. Else validation is passed. if ( empty(PostData::$postData[$attributes[1]]) // empty conditioned field && ( !isset(PostData::$postData[$fieldName]) // empty required field || '' === PostData::$postData[$fieldName] ) ) { $this->_error($this->errorMsgs['required_item'], $fieldName); } } elseif ( !isset(PostData::$postData[$fieldName]) || '' === PostData::$postData[$fieldName] ) { // if there is no field or the field is empty, write error $this->_error($this->errorMsgs['required_item'], $fieldName); } } private function ruleBail($fieldName, $ruleData) { $this->shouldBail = true; } /** * validation method according to the 'numeric' rule. * * @param string $fieldName * @param array $ruleData */ private function ruleNumeric($fieldName, $ruleData) { // if the field exists, then the data type of the field if (true == isset(PostData::$postData[$fieldName])) { // if the data type is not a number, then write an error if (false == is_numeric(PostData::$postData[$fieldName])) { $this->_error($this->errorMsgs['numeric'], $fieldName); } } } /** * validation method according to the 'boolean' rule. * * @param string $fieldName * @param array $ruleData */ private function ruleBoolean($fieldName, $ruleData) { // if the field exists, then the data type of the field if (true == isset(PostData::$postData[$fieldName])) { // if the data type is not a boolean, then write an error if (false == is_bool(PostData::$postData[$fieldName])) { $this->_error($this->errorMsgs['boolean'], $fieldName); } } } /** * validation method according to the 'array' rule. * * @param string $fieldName * @param array $ruleData */ private function ruleArray($fieldName, $ruleData) { // if the field exists, then the data type of the field if (true == isset(PostData::$postData[$fieldName])) { // if the data type is not a array, then write an error if (false == is_array(PostData::$postData[$fieldName])) { $this->_error($this->errorMsgs['array'], $fieldName); } } } /** * validation method according to the 'in' rule. * * @param string $fieldName * @param array $ruleData */ private function ruleIn($fieldName, $ruleData) { // if the field exists and has attributes, then the data type of the field if (isset(PostData::$postData[$fieldName]) && isset($ruleData['attributes'])) { // if the value of the field is not among the value specified in the attributes, then write an error if (false == in_array(PostData::$postData[$fieldName], $ruleData['attributes'])) { $this->_error($this->errorMsgs['in'], $fieldName); } } } /** * Check for valid xml. * * @param $fieldName * @param $ruleData */ private function ruleXml($fieldName, $ruleData) { // if the field exists if (isset(PostData::$postData[$fieldName])) { // Save old setting value $useErrors = libxml_use_internal_errors(true); // Try to load value as XML $doc = new \DOMDocument(); $success = $doc->loadXML(''.PostData::$postData[$fieldName].''); // Errors has occurred if (!$success) { $this->_error($this->errorMsgs['xml'], $fieldName); } // Restore setting value libxml_use_internal_errors($useErrors); } } /** * method of filling in data validation errors. * * @param string $errorMsg * @param string $fieldName */ private function _error($errorMsg, $fieldName) { $this->errors[] = sprintf($errorMsg, $fieldName); } }