Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Validation required for hidden element field not showing on save configuration

Solved by Karmalakas View solution

Started by Pedro M 1 year ago · 7 replies · 97 views
1 year ago

Hello.
I have a situation I don't know how to solve. In the Orestbida Cookie Consent plugin, in the content section of the admin panel, I have an Elements field, which has several subfields, each with two options: Usage Title and Description. The issue is that in one of the elements (Necessary Cookies), these fields are required in the plugin's blueprints.yaml, but since the Necessary Cookies section isn't visible by default (it's the second one in the elements combobox), saving the plugin configuration doesn't show any errors, and I'd like it to show an error at the top, just like when saving the content of a plugin or theme. Otherwise, the only way to know if these fields are required is to select the Necessary Cookies item in the elements combobox, and then if I click Save, it indicates that they are required in the text boxes for that element.

I've tried something with onAdminSave, but I don't think it's the right approach, as form field validation is performed before the configuration is saved.

Could you provide me with a solution to this issue?

Here are some screenshots:

orestbida1|690x320

orestbida2|690x290

YAML
fields:
            usage:
              type: element
              fields:
                cookies_sections.usage.title:
                  type: text
                  size: large
                  label: PLUGIN_ORESTBIDA_COOKIE_CONSENT.USAGE_TITLE
                  placeholder: PLUGIN_ORESTBIDA_COOKIE_CONSENT.USAGE_PLACEHOLDER
                cookies_sections.usage.description:
                  type: textarea
                  rows: 3
                  markdown: true
                  label: PLUGIN_ORESTBIDA_COOKIE_CONSENT.DESCRIPTION_LABEL
                  placeholder: PLUGIN_ORESTBIDA_COOKIE_CONSENT.DESCRIPTION_PLACEHOLDER
            necessary:
              type: element
              fields:
                cookies_sections.necessary.title:
                  type: text
                  label: PLUGIN_ORESTBIDA_COOKIE_CONSENT.NECESSARY_TITLE
                  placeholder: PLUGIN_ORESTBIDA_COOKIE_CONSENT.NECESSARY_PLACEHOLDER
                  validate:
                    type: text
                    required: true
                cookies_sections.necessary.description:
                  type: textarea
                  rows: 3
                  markdown: true
                  label: PLUGIN_ORESTBIDA_COOKIE_CONSENT.DESCRIPTION_LABEL
                  placeholder: PLUGIN_ORESTBIDA_COOKIE_CONSENT.DESCRIPTION_PLACEHOLDER
                  validate:
                    type: text
                    required: true
1 year ago

It's HTML5 validation and not much you can do about it, except disabling it completely:

HTML
<form method="post" action="/" novalidate>...</form>

Then form will be submitted and validation would happen server-side

1 year ago

@pmoreno, well, elements is a conditional field, and its behavior is as intended

👍 1
1 year ago

Thanks @Karmalakas y @b.da for your feedback. I may need to change the field type in which this content is displayed. Any suggestions?

1 year ago Solution

@pmoreno:
Any suggestions?

Show all 4 fields in 2 columns

👍 1
1 year ago

That's exactly what I was starting to test. It might be the best way for the user to check if they're missing a required field.

1 year ago

Honestly, I can't imagine a situation where anyone would want to hide mandatory fields. That's probably one of the worst UX 🤔

Users should not need to check if they didn't fill something, that's required. It should be thrown in their face 😀 I've seen some forms at the bottom of the page and when submitted, page reloads at the top. Terrible experience...

1 year ago

You're absolutely right, and I'm trying to make sure users don't have that experience, but I haven't used the element field before, and I thought it would behave the same as a normal form, displaying the required field message, even though it would be displayed when clicking on a combo box option.

I'm definitely going to change it so everything works as it should.

Thanks for everything.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 95 15 hours ago
General · by pamtbaau, 20 hours ago
1 61 19 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 356 5 days ago
General · by Duc , 6 days ago
3 44 6 days ago