/** * Return array for filter users * * @return array */ public function getFilter() { $return = []; if ($this->isValid()) { foreach ($this->fields as $field) { if (Str::length($field->getValue()) > 0) { if ($field instanceof TextField) { $return['%' . $field->getName()] = $field->getValue(); } elseif ($field instanceof DateField) { if (Str::endsWith($field->getName(), '_FROM')) { $key = Str::substr($field->getName(), 0, Str::length($field->getName()) - 5); $return['>=' . $key] = $field->getValue(); } elseif (Str::endsWith($field->getName(), '_TO')) { $key = Str::substr($field->getName(), 0, Str::length($field->getName()) - 3); $return['<=' . $key] = $field->getValue(); } else { $return[$key] = $field->getValue(); } } else { $return[$field->getName()] = $field->getValue(); } } } } else { if ($this->getErrors()->has()) { $mess = implode(',', $this->getErrors()->all()); throw new \RuntimeException('Filter error: ' . $mess); } } return $return; }
/** * Check is form data is valid * * @param boolean $new * @return boolean */ public function isValid($new = true) { if ($this->fields === null) { $this->completeFields(); } $post = $this->getDataFromRequest(); if (is_array($post)) { $error = $this->checkInputData($post, $new); foreach ($post as $key => &$value) { if (!$error->has($key)) { $func = 'validate'; foreach (explode('_', $key) as $item) { $func .= Str::ucwords(Str::toLower($item)); } if (method_exists($this, $func)) { $return = $this->{$func}($value); if (Str::length($return) > 0) { $error->add($key, $return); } } } } unset($value); if (!$error->has()) { if ($this->onValidate()) { $this->data = $post; return true; } } $this->error = $error; foreach ($error->toArray() as $key => $message) { $this->fields->get($key)->error[] = $message; } } else { $this->error = new MessageBag(); foreach ($this->default as $key => $value) { if ($this->fields->has($key)) { $this->fields->get($key)->setValue($value); } } } return false; }
/** * Add error * @param string $key * @param string $message * @param array $params */ public function addError($key, $message, $params = []) { if ($this->error === null) { $this->error = new MessageBag(); } $this->error->add(Str::toUpper($key), !empty($params) ? strtr($message, $params) : $message); }