(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
## 06/27/2023

View File

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

View File

@ -518,7 +518,7 @@ class FormPlugin extends Plugin
$captcha_value = trim($form->value('basic-captcha'));
if (!$captcha->validateCaptcha($captcha_value)) {
$message = $params['message'] ?? $this->grav['language']->translate('PLUGIN_FORM.ERROR_BASIC_CAPTCHA');
$form->setData('basic-captcha', '');
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
'message' => $message
@ -667,6 +667,11 @@ class FormPlugin extends Plugin
$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 */
$twig = $this->grav['twig'];
$vars = [
@ -1130,6 +1135,10 @@ class FormPlugin extends Plugin
return false;
}
if (isset($form->xhr_submit) && $form->xhr_submit) {
$form->set('template', $form->template ?? 'form-xhr');
}
// Set page template if passed by form
if (isset($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 layout = layout ?? form.layout ?? 'default' %}
{% set field_layout = field_layout ?? layout %}

View File

@ -89,7 +89,14 @@
{{ macro.preview(path, file, _context) }}
{% endfor %}
{% include 'forms/fields/hidden/hidden.html.twig' with {field: {name: '_json.' ~ field.name}, value: (value ?? [])|json_encode } %}
</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 %}
{% 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
#}
{% 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 %}