Kirby-uniform: forms in controllers.php ≠ template.php

uniform

#21

Silly error, was using the same name for a field… now working ofc.


Last thing is to put error messages with Kirby Builder based on the selected field.


#22

Whenever the form validation failed or anything else went wrong, the user would loose all the form data that was already entered. Of course we don’t want that so Uniform flashes all form data to the session in this case. The flashed data can be accessed through the old method to re-populate the form data as you can see in the example below:

@mzur How would I access

value="<?php echo $form->old('email'); ?>

if I am using Kirby Builder / a structure field?

Something like this, but needs to fetch the old('value') instead of the default one?

value="<?= $data->type() ?>" 

Where type is email, ‘text’, etc—the kind of field I am using.

I’d like to go step by step and fully setup your plugin w/ a panel-centric way of building the form, as I think it might come handy also for other people.

Any more help appreciated :smiley:


#23

Don’t know about this last bit at the mo, but I think all this $page->site() stuff is completely unnecessary as is converting the structure to an array. To avoid an object as array key, all I think you have to do is to get the value:

$rules = [];
  foreach($page->builder()->toStructure() as $section) {
    $rules[$section->_fieldset()->value()] = $section->rules();
}
var_dump($rules);

(not tested)


#24

@texnixe’s code looks good. The structure field won’t return the actual string of the field content but an object. I didn’t think of that.

Regarding $form->old(): It’s all about the form field names. If you have a field with name $section->_fieldset()->value() then you get the old value with the same key:

<input name="<?php echo $section->_fieldset()->value() ?>" value="<?php echo $form->old($section->_fieldset()->value()) ?>">

#25

I tested it and I get the same initial error:

Illegal Offset Type


#26

Thank you @mzur!

Tested this as well w/ a structured field (no Kirby Builder) and I get this

Illegal offset type in isset or empty

on line 140 of site/plugins/uniform/vendor/mzur/kirby-form/src/Form.php

// Encode HTML entities for output
        return isset($data[$key]) ? $this->encodeField($data[$key]) : '';
    }

#27

That’s the same error for both cases. What do you get with var_dump($section->_fieldset())? Also, try casting the key to a string with strval($section->_fieldset()).


#28

This is what I get

array(1) { ["placeholder"]=> object(Field)#210 (3) { ["page"]=> string(19) "partecipa/iscriviti" ["key"]=> string(5) "rules" ["value"]=> NULL } }

I have only one _fieldset in the kirby builder, and a bunch of subfield to pick from in order to set the input type.

Also, when I merge the form array with another custom array made in the controller, and then I make a new Form($rules), only the added keys from the non-form array get added to the logfile, and not their values.

If I check the merged array before doing new Form I get everything, but after the new Form command only the keys from $info.

Is it Uniform specific or due to the new Form command?

$survey_num = page('survey')->children()->count() +1;
$info = array(
  'title' => 'Questionario #' . $survey_num,
  'date'  => date('Y-m-d, H:i:s'),
);

$rules = $page->inquiry()->toStructure()->toArray();
foreach($_POST as $key => $value) {
  if($key != 'csrf_token' && $key != 'website') {
    $rules[$key] = esc($value);
  }
};

$rules = a::merge($info, $rules);
a::show($rules);
$form = new Form($rules);

#29

That’s confusing. How does the content file of the page look like? It should look like this with one _fieldset for each form field.

Also: How do you generate the form field names in the template? More specifically:

<input type="text" name="<?php WHAT DO YOU HAVE HERE? ?>">

And finally: Please read the Uniform docs carefully again. The rules array you are constructing there does not make any sense.


#30

I think it would also be helpful to have your blueprint together with some example content, so that we know what all this refers to.