grav-plugin-form icon indicating copy to clipboard operation
grav-plugin-form copied to clipboard

Allow to skip field rendering still allowing its server-side processing

Open drzraf opened this issue 5 years ago • 4 comments

A custom admin-field I developped is dynamically creating subfields on the fly.

For example:

    header.form.field_selection:
      type: my-custom-type
      fields:
        firstname:
          content: First name

would submit data[header][form][firstname] and data[header][form][_is_required][firstName] = 0 or 1

Since _is_required is not known to the blueprint, it ends-up in blueprint.extra, thus creating a hidden element under [data][_json] which, sadly, always take-over my submitted values => data is not saved.

In order to avoid that, I added a:

    header.form._is_required:
      type: hidden

to my form.

The problem is that with such a hidden field, the POST submission looks like:

data[header][form][_required] = ""
data[header][form][_required][firstname] = 1

=> data is not saved because the array structure is disregarded.

drzraf avatar Dec 12 '19 14:12 drzraf

Those extra inputs must be outside of data. You still have access to them when handling the form.

mahagr avatar Dec 13 '19 08:12 mahagr

Yes, but since these extra are forcefully output as hidden fields, I can hardly provide myself an extra widget to override them. For example:

<input type="TEXT" id="my-widget" name="data[_json][form][header][_is_required][foo]" value="1" />
<input data-grav-field="HIDDEN" data-grav-disabled="false" type="hidden" class="input" name="data[_json][form][header][_is_required][foo]" value="&quot;1&quot;">

Since the hidden fields are output after in the <form> it's probable (although webserver-dependant), that previous values are ignored.

  • There is another bug in that the value is nested between extra-escaped-quotes: &quot;
  • There is no known way to keep extra from appearing as a set of hidden fields and I've tried a bunch after digging system/src/Grav/Common/Data/Validation.php:
    • Using header.form._is_required.'': { type: hidden }: Still considered extra: DOES NOT WORK
    • Using (sub)fields to declare and impose and array structure: DOES NOT WORK
    • Using type: checkbox, validate.ignore: yes to avoid rendering: Normal field rendering is skipped, but so is submission processing (field is not stored): DOES NOT WORK

Let's restate: I'd like my custom field: to be posted, to be processed according to a blueprint if one exist, to be stored. When rendering the values, I'd like it to be skipped. One way would be:

  my_fields_*: # A regexp
     type: checkbox  # Allow an array to be passed
     display: skip # field is not rendered automatically. Author takes care of it. It's not part of `_json` or so, not even `hidden`. (All the rest is as usual normal.)

drzraf avatar Dec 13 '19 13:12 drzraf

ping?

drzraf avatar Jul 14 '20 02:07 drzraf

Oh sorry. The unknown values are all JSON encoded. This is to allow any kind of data to be represented. In the regular forms, these hidden fields are needed as the file gets saved as it is and not having these values would remove them.

If you need extra data in the form which you'd like to override, just provide/add them as known fields and handle them in your code. You can add extra fields to the blueprints via events and they do not need to be part of data.

mahagr avatar Jul 24 '20 09:07 mahagr