diff --git a/assets/controllers/helpers/form_cleanup_controller.js b/assets/controllers/helpers/form_cleanup_controller.js new file mode 100644 index 00000000..955c1e09 --- /dev/null +++ b/assets/controllers/helpers/form_cleanup_controller.js @@ -0,0 +1,43 @@ +import {Controller} from "@hotwired/stimulus"; + +/** + * Purpose of this controller is to clean up the form before it is finally submitted. This means empty fields get disabled, so they are not submitted. + * This is especially useful for GET forms, to prevent very long URLs + */ +export default class extends Controller { + + /** + * Call during the submit event of the form. This will disable all empty fields, so they are not submitted. + * @param event + */ + submit(event) { + /** Find the form this event belongs to */ + /** @type {HTMLFormElement} */ + const form = event.target.closest('form'); + + for(const element of form.elements) { + if(! element.value) { + element.disabled = true; + } + + //Workaround for tristate checkboxes which use a hidden field to store the value + if ((element.type === 'hidden' || element.type === 'checkbox') && element.value === 'indeterminate') { + element.disabled = true; + } + } + } + + /** + * Submits the form with all form elements disabled, so they are not submitted. This is useful for GET forms, to reset the form to not filled state. + * @param event + */ + clearAll(event) + { + const form = event.target.closest('form'); + for(const element of form.elements) { + element.disabled = true; + } + + form.submit(); + } +} \ No newline at end of file diff --git a/src/Form/Filters/PartFilterType.php b/src/Form/Filters/PartFilterType.php index 968187ac..b3d491ab 100644 --- a/src/Form/Filters/PartFilterType.php +++ b/src/Form/Filters/PartFilterType.php @@ -210,11 +210,11 @@ class PartFilterType extends AbstractType ]); $builder->add('submit', SubmitType::class, [ - 'label' => 'Update', + 'label' => 'filter.submit', ]); - $builder->add('reset', ResetType::class, [ - 'label' => 'Reset', + $builder->add('discard', ResetType::class, [ + 'label' => 'filter.discard', ]); } diff --git a/src/Form/Type/TriStateCheckboxType.php b/src/Form/Type/TriStateCheckboxType.php index 87bdc473..9b3b2514 100644 --- a/src/Form/Type/TriStateCheckboxType.php +++ b/src/Form/Type/TriStateCheckboxType.php @@ -171,10 +171,10 @@ final class TriStateCheckboxType extends AbstractType implements DataTransformer case 'true': return true; case 'false': - case '': return false; case 'indeterminate': case 'null': + case '': return null; default: throw new InvalidArgumentException('Invalid value encountered!: '.$value); diff --git a/templates/Parts/lists/_filter.html.twig b/templates/Parts/lists/_filter.html.twig index 953ce0d6..aa2eb474 100644 --- a/templates/Parts/lists/_filter.html.twig +++ b/templates/Parts/lists/_filter.html.twig @@ -1,5 +1,5 @@