(Grav GitSync) Automatic Commit from exu

This commit is contained in:
exu 2023-12-22 13:26:01 +01:00 committed by GitSync
parent d90fc4333b
commit 18e6688d1f
8 changed files with 61 additions and 2 deletions

View File

@ -1,3 +1,18 @@
# v7.3.0
## 12/14/2023
1. [](#new)
* Added XHR/Ajax form submission as an option in the form blueprint. See [Learn Forms](https://learn.getgrav.org/17/forms/forms/how-to-ajax-submission) for details.
# v7.2.2
## 12/13/2023
1. [](#improved)
* Add _inline errors_ for `file` field. Useful in combination with `form: no-validate: true` form setting.
* Validate filename against `uploads_dangerous_extensions` when using the `save:` action
1. [](#bugfix)
* Cleared 'basic captcha' value when invalid
# v7.2.1 # v7.2.1
## 06/27/2023 ## 06/27/2023

View File

@ -1,7 +1,7 @@
name: Form name: Form
slug: form slug: form
type: plugin type: plugin
version: 7.2.1 version: 7.3.0
description: Enables forms handling and processing description: Enables forms handling and processing
icon: check-square icon: check-square
author: author:

View File

@ -518,7 +518,7 @@ class FormPlugin extends Plugin
$captcha_value = trim($form->value('basic-captcha')); $captcha_value = trim($form->value('basic-captcha'));
if (!$captcha->validateCaptcha($captcha_value)) { if (!$captcha->validateCaptcha($captcha_value)) {
$message = $params['message'] ?? $this->grav['language']->translate('PLUGIN_FORM.ERROR_BASIC_CAPTCHA'); $message = $params['message'] ?? $this->grav['language']->translate('PLUGIN_FORM.ERROR_BASIC_CAPTCHA');
$form->setData('basic-captcha', '');
$this->grav->fireEvent('onFormValidationError', new Event([ $this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form, 'form' => $form,
'message' => $message 'message' => $message
@ -667,6 +667,11 @@ class FormPlugin extends Plugin
$filename = $prefix . $this->udate($format, $raw_format) . $postfix . $ext; $filename = $prefix . $this->udate($format, $raw_format) . $postfix . $ext;
} }
// Handle bad filenames.
if (!Utils::checkFilename($filename)) {
throw new RuntimeException(sprintf('Form save: File with extension not allowed: %s', $filename));
}
/** @var Twig $twig */ /** @var Twig $twig */
$twig = $this->grav['twig']; $twig = $this->grav['twig'];
$vars = [ $vars = [
@ -1130,6 +1135,10 @@ class FormPlugin extends Plugin
return false; return false;
} }
if (isset($form->xhr_submit) && $form->xhr_submit) {
$form->set('template', $form->template ?? 'form-xhr');
}
// Set page template if passed by form // Set page template if passed by form
if (isset($form->template)) { if (isset($form->template)) {
$this->grav['page']->template($form->template); $this->grav['page']->template($form->template);

View File

@ -0,0 +1 @@
{% extends "forms/default/form.html.twig" %}

View File

@ -1,3 +1,4 @@
{% block xhr %}{% endblock %}
{% set form = form ?? grav.session.getFlashObject('form') %} {% set form = form ?? grav.session.getFlashObject('form') %}
{% set layout = layout ?? form.layout ?? 'default' %} {% set layout = layout ?? form.layout ?? 'default' %}
{% set field_layout = field_layout ?? layout %} {% set field_layout = field_layout ?? layout %}

View File

@ -89,7 +89,14 @@
{{ macro.preview(path, file, _context) }} {{ macro.preview(path, file, _context) }}
{% endfor %} {% endfor %}
{% include 'forms/fields/hidden/hidden.html.twig' with {field: {name: '_json.' ~ field.name}, value: (value ?? [])|json_encode } %} {% include 'forms/fields/hidden/hidden.html.twig' with {field: {name: '_json.' ~ field.name}, value: (value ?? [])|json_encode } %}
</div> </div>
{% if inline_errors and errors %}
<div class="{{ form_field_inline_error_classes }}">
<p class="form-message"><i class="fa fa-exclamation-circle"></i> {{ errors|first|raw }}</p>
</div>
{% endif %}
{% if grav.browser.browser == 'msie' and grav.browser.version < 12 %} {% if grav.browser.browser == 'msie' and grav.browser.version < 12 %}
{% do assets.addJs('plugin://form/assets/object.assign.polyfill.js') %} {% do assets.addJs('plugin://form/assets/object.assign.polyfill.js') %}

View File

@ -26,4 +26,9 @@ You can also override individual fields by copying (using text field as an examp
templates/forms/fields/text/text.html.twig -> templates/forms/fields/text/tailwind-text.html.twig templates/forms/fields/text/text.html.twig -> templates/forms/fields/text/tailwind-text.html.twig
#} #}
{% extends "forms/default/form.html.twig" %} {% extends "forms/default/form.html.twig" %}
{% block xhr %}
{% include 'forms/layouts/xhr.html.twig' %}
{% endblock %}

View File

@ -0,0 +1,21 @@
{% if form.xhr_submit == true %}
{% do assets.addInlineJs("
document.addEventListener('DOMContentLoaded', function() {
var form = document.getElementById('" ~ form.id ~ "');
form.addEventListener('submit', function(e) {
// prevent standard form submission
e.preventDefault();
// submit the form via Ajax
var xhr = new XMLHttpRequest();
xhr.open(form.getAttribute('method'), form.getAttribute('action'));
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if (xhr.status === 200) {
document.getElementById('" ~ form.id ~ "').innerHTML = xhr.responseText;
}
};
xhr.send(new URLSearchParams(new FormData(form)).toString());
});
});
", {'group': 'bottom', 'position': 'before', 'priority': 100}) %}
{% endif %}