/** * Handle form processing on POST action. */ public function post() { $grav = Grav::instance(); $uri = $grav['uri']->url; $session = $grav['session']; if (isset($_POST)) { $this->values = new Data(isset($_POST) ? (array) $_POST : []); $data = $this->values->get('data'); // Add post data to form dataset if (!$data) { $data = $this->values->toArray(); } if (method_exists('Grav\\Common\\Utils', 'getNonce')) { if (!$this->values->get('form-nonce') || !Utils::verifyNonce($this->values->get('form-nonce'), 'form')) { $event = new Event(['form' => $this, 'message' => $grav['language']->translate('PLUGIN_FORM.NONCE_NOT_VALIDATED')]); $grav->fireEvent('onFormValidationError', $event); return; } } $i = 0; foreach ($this->items['fields'] as $key => $field) { $name = isset($field['name']) ? $field['name'] : $key; if (!isset($field['name'])) { if (isset($data[$i])) { //Handle input@ false fields $data[$name] = $data[$i]; unset($data[$i]); } } if ($field['type'] == 'checkbox') { $data[$name] = isset($data[$name]) ? true : false; } $i++; } $this->data->merge($data); } // Validate and filter data try { $this->data->validate(); $this->data->filter(); $grav->fireEvent('onFormValidationProcessed', new Event(['form' => $this])); } catch (\RuntimeException $e) { $event = new Event(['form' => $this, 'message' => $e->getMessage(), 'messages' => $e->getMessages()]); $grav->fireEvent('onFormValidationError', $event); if ($event->isPropagationStopped()) { return; } } // Process previously uploaded files for the current URI // and finally store them. Everything else will get discarded $queue = $session->getFlashObject('files-upload'); $queue = $queue[base64_encode($uri)]; if (is_array($queue)) { foreach ($queue as $key => $files) { foreach ($files as $destination => $file) { if (!rename($file['tmp_name'], $destination)) { throw new \RuntimeException(sprintf($grav['language']->translate('PLUGIN_FORM.FILEUPLOAD_UNABLE_TO_MOVE', null, true), '"' . $file['tmp_name'] . '"', $destination)); } unset($files[$destination]['tmp_name']); } $this->data->merge([$key => $files]); } } $process = isset($this->items['process']) ? $this->items['process'] : []; if (is_array($process)) { $event = null; foreach ($process as $action => $data) { if (is_numeric($action)) { $action = \key($data); $data = $data[$action]; } $previousEvent = $event; $event = new Event(['form' => $this, 'action' => $action, 'params' => $data]); if ($previousEvent) { if (!$previousEvent->isPropagationStopped()) { $grav->fireEvent('onFormProcessed', $event); } else { break; } } else { $grav->fireEvent('onFormProcessed', $event); } } } else { // Default action. } }