_fields = $fields; $this->_errorFields = []; $this->_isLocaleField = $isLocaleField; $this->_userFunction = $userFunction; $this->_additionalArguments = $additionalArguments; $this->_complementReturn = $complementReturn; } // // Setters and Getters // /** * Get array of fields where an error occurred. * * @return array */ public function getErrorFields() { return $this->_errorFields; } /** * Is it a multilingual-capable field. * * @return bool */ public function isLocaleField() { return $this->_isLocaleField; } // // Public methods // /** * @see FormValidator::isValid() * * @return bool */ public function isValid() { if ($this->isEmptyAndOptional()) { return true; } $data = $this->getFieldValue(); if (!is_array($data)) { return false; } $isValid = true; foreach ($data as $key => $value) { // Bypass check for empty sub-fields if validation type is "optional" if ($this->getType() == FormValidator::FORM_VALIDATOR_OPTIONAL_VALUE && ($value == [] || $value == '')) { continue; } if (count($this->_fields) == 0) { if ($this->isLocaleField()) { $ret = call_user_func_array($this->_userFunction, array_merge([$value, $key], $this->_additionalArguments)); } else { $ret = call_user_func_array($this->_userFunction, array_merge([$value], $this->_additionalArguments)); } $ret = $this->_complementReturn ? !$ret : $ret; if (!$ret) { $isValid = false; if ($this->isLocaleField()) { $this->_errorFields[$key] = $this->getField() . "[{$key}]"; } else { array_push($this->_errorFields, $this->getField() . "[{$key}]"); } } } else { // In the two-dimensional case we always expect a value array. if (!is_array($value)) { $isValid = false; if ($this->isLocaleField()) { $this->_errorFields[$key] = $this->getField() . "[{$key}]"; } else { array_push($this->_errorFields, $this->getField() . "[{$key}]"); } continue; } foreach ($this->_fields as $field) { // Bypass check for empty sub-sub-fields if validation type is "optional" if ($this->getType() == FormValidator::FORM_VALIDATOR_OPTIONAL_VALUE) { if (!isset($value[$field]) || $value[$field] == [] or $value[$field] == '') { continue; } } else { // Make sure that we pass in 'null' to the user function // if the expected field doesn't exist in the value array. if (!array_key_exists($field, $value)) { $value[$field] = null; } } if ($this->isLocaleField()) { $ret = call_user_func_array($this->_userFunction, array_merge([$value[$field], $key], $this->_additionalArguments)); } else { $ret = call_user_func_array($this->_userFunction, array_merge([$value[$field]], $this->_additionalArguments)); } $ret = $this->_complementReturn ? !$ret : $ret; if (!$ret) { $isValid = false; if ($this->isLocaleField()) { if (!isset($this->_errorFields[$key])) { $this->_errorFields[$key] = []; } array_push($this->_errorFields[$key], $this->getField() . "[{$key}][{$field}]"); } else { array_push($this->_errorFields, $this->getField() . "[{$key}][{$field}]"); } } } } } return $isValid; } /** * Is the field an array. * * @return bool */ public function isArray() { return is_array($this->getFieldValue()); } } if (!PKP_STRICT_MODE) { class_alias('\PKP\form\validation\FormValidatorArrayCustom', '\FormValidatorArrayCustom'); }