errors->no()) { $this->submitBlocks(); } } /** * Submit sub blocks * @return void */ public function submitBlocks() { $this->blocksIterator(function ($block) { if ($block instanceof FormBlock) { $block->submit(); } }); } /** * Set/get response data * @param array|string $key * @param mixed $value * @param bool $extend * @return mixed */ public function respond($key, $value = null, $extend = true) { # Combine form blocks response if ($this->form && $this->form !== $this) { return $this->form->respond($key, $value, $extend); } return parent::respond($key, $value, $extend); } /** * Get response data as Response * @param array $response * @return \bff\http\Response */ public function getActionResponse(array $response = []) { # Combine form blocks response if ($this->form && $this->form !== $this) { return $this->form->getActionResponse($response); } return parent::getActionResponse($response); } /** * Validate * @param array $data * @return array */ public function validate($data = []) { return $this->validateBlocks( $this->validateUsingRules($data) ); } /** * Validate request data using rules * @param array $data to extend * @param array|null $rules * @return array */ public function validateUsingRules(array $data = [], ?array $rules = null) { if (is_null($rules)) { $rules = $this->rules(); } $rules = $this->app->filter('view.block.validate.rules', $rules, $this); if (is_array($rules) && $rules) { foreach ($rules as $name => $rule) { if (array_key_exists($name, $data)) { $this->input->clean($data[$name], $rule); } else { $data[$name] = &$this->linkValidated($name, $this->request->input()->postget($name, $rule)); } } } return $this->app->filter('view.block.validate', $data, $this); } /** * Link validated data * @param string $name * @param mixed $value * @return mixed */ public function &linkValidated($name, $value) { if (property_exists($this, $name) && ! $this->isProtectedSetting($name)) { $this->$name = $value; return $this->$name; } $this->data[$name] = $value; return $this->data[$name]; } /** * Validate sub blocks * @param array $data * @return array */ public function validateBlocks(array $data = []) { $this->blocksIterator(function ($block) use (&$data) { if ($block instanceof FormBlock) { $block->setRequest($this->getRequest()); $data = $block->validate($data); } }); return $data; } /** * Get block declared validation rules * @return array */ public function rules(): array { return []; } /** * @param Block|mixed $parent * @return static */ public function setParent($parent) { parent::setParent($parent); if ($parent instanceof Form) { $this->form = $parent; } else { $this->form = $this->getParentForm(); } return $this; } /** * Get parent Form * @return Form|null */ public function getParentForm() { if ($this instanceof Form) { return $this; } $parent = $this->getParent(); if ($parent instanceof FormBlock) { return $parent->getParentForm(); } return null; } /** * Handle form reload block * @param $event string * @return string|bool */ public function reloadBlock($event) { if (is_callable($this->onReloadBlock)) { return call_user_func($this->onReloadBlock, $event); } if ($this->visible()) { return $this->render(); } return false; } /** * Detect if block enable in reload handle * @return bool */ public function visible() { if (is_callable($this->onVisible)) { return call_user_func($this->onVisible); } return false; } /** * Load data * @var array $data */ public function onLoad($data = []) { $this->blocksIterator(function ($block) use (&$data) { if ($block instanceof FormBlock) { $block->onLoad($data); } }); } }