(Grav GitSync) Automatic Commit from smokephil

This commit is contained in:
smokephil 2025-11-11 19:27:29 +01:00 committed by GitSync
parent d73d0ba519
commit 96a01e3ab4
260 changed files with 25905 additions and 16011 deletions

View file

@ -550,6 +550,10 @@ class Form implements FormInterface, ArrayAccess
$url = $uri->url;
$post = $uri->post();
if (!empty($post['__unique_form_id__'])) {
$this->setUniqueId($post['__unique_form_id__']);
}
$name = $post['name'] ?? null;
$task = $post['task'] ?? null;
@ -661,6 +665,7 @@ class Form implements FormInterface, ArrayAccess
// Handle file size limits
$settings->filesize *= self::BYTES_TO_MB; // 1024 * 1024 [MB in Bytes]
if ($settings->filesize > 0 && $upload['file']['size'] > $settings->filesize) {
$grav['log']->warning(sprintf('Form upload rejected: %s (%d bytes) exceeds limit %d bytes', $filename, $upload['file']['size'], $settings->filesize));
// json_response
return [
'status' => 'error',
@ -748,7 +753,7 @@ class Form implements FormInterface, ArrayAccess
* @param Language|null $language
* @return string File upload error message
*/
public function getFileUploadError(int $error, Language $language = null): string
public function getFileUploadError(int $error, ?Language $language = null): string
{
if (!$language) {
$grav = Grav::instance();
@ -892,6 +897,16 @@ class Form implements FormInterface, ArrayAccess
$this->data->merge($data);
}
if (!empty($post['__unique_form_id__'])) {
$this->setUniqueId($post['__unique_form_id__']);
}
// Ensure file field values are populated from the flash storage before validation.
$flash = $this->getFlash();
if ($flash->exists()) {
$this->setAllFiles($flash);
}
// Validate and filter data
try {
$grav->fireEvent('onFormPrepareValidation', new Event(['form' => $this]));
@ -899,17 +914,41 @@ class Form implements FormInterface, ArrayAccess
$this->data->validate();
$this->data->filter();
$grav->fireEvent('onFormValidationProcessed', new Event(['form' => $this]));
} catch (ValidationException $e) {
$this->status = 'error';
$event = new Event(['form' => $this, 'message' => $e->getMessage(), 'messages' => $e->getMessages()]);
$grav->fireEvent('onFormValidationError', $event);
if ($event->isPropagationStopped()) {
return;
// Add special handling for file/filepond fields
foreach ($this->fields as $field) {
// Don't restrict to just type=file, but also other file based as long as they have filesize && accept
if (isset($field['filesize']) && isset($field['accept']) &&
isset($field['validate']['required']) &&
$field['validate']['required']) {
// Get field name
$fieldName = $field['name'];
$fieldLabel = $field['label'] ?? $field['name'];
// Check if files exist in the session for this field
$flashObject = $this->getFlash();
if ($flashObject->exists()) {
$filesInField = $flashObject->getFilesByField($fieldName);
// If no files found, add validation error
if (empty($filesInField)) {
$this->setError("$fieldLabel " . $grav['language']->translate("PLUGIN_FORM.FIELD_REQUIRED"));
throw new ValidationException();
}
} else {
// No flash object with files found
$this->setError("$fieldLabel " . $grav['language']->translate("PLUGIN_FORM.FIELD_REQUIRED"));
throw new ValidationException();
}
}
}
} catch (RuntimeException $e) {
$grav->fireEvent('onFormValidationProcessed', new Event(['form' => $this]));
} catch (ValidationException | RuntimeException $e) {
$this->status = 'error';
$event = new Event(['form' => $this, 'message' => $e->getMessage(), 'messages' => []]);
$this->message = $this->message ?? $e->getMessage();
$this->messages = array_merge($this->messages, $e->getMessages());
$event = new Event(['form' => $this, 'message' => $this->message, 'messages' => $this->messages]);
$grav->fireEvent('onFormValidationError', $event);
if ($event->isPropagationStopped()) {
return;
@ -918,10 +957,11 @@ class Form implements FormInterface, ArrayAccess
$redirect = $redirect_code = null;
$process = $this->items['process'] ?? [];
$legacyUploads = !isset($process['upload']) || $process['upload'] !== false;
$legacyUploads = !isset($process['upload']) || $process['upload'] !== true;
if ($legacyUploads) {
$this->legacyUploads();
$this->copyFiles();
}
if (is_array($process)) {
@ -949,10 +989,6 @@ class Form implements FormInterface, ArrayAccess
}
}
if ($legacyUploads) {
$this->copyFiles();
}
$this->getFlash()->delete();
if ($redirect) {
@ -1235,6 +1271,10 @@ class Form implements FormInterface, ArrayAccess
$post = $uri->post();
$post['data'] = $this->decodeData($post['data'] ?? []);
if (!empty($post['__unique_form_id__'])) {
$this->setUniqueId($post['__unique_form_id__']);
}
if (empty($post['form-nonce']) || !Utils::verifyNonce($post['form-nonce'], 'form')) {
throw new RuntimeException('Bad Request: Nonce is missing or invalid', 400);
}
@ -1256,7 +1296,7 @@ class Form implements FormInterface, ArrayAccess
* @param string|null $field
* @return void
*/
protected function removeFlashUpload(string $filename, string $field = null)
protected function removeFlashUpload(string $filename, ?string $field = null)
{
$flash = $this->getFlash();
$flash->removeFile($filename, $field);