Extend existing form defined in forms.php

edited May 2016 in Modules
Hey everyone,

I have a form defined almost completely in forms.php, and it works great. However, there is one extra field I want to add to it that is dependent on the page the user is at.

Specifically, there is a controller called 'autos', and in its 'view' method I get the form, and add it to my $vars array, where it is then sent to the page.

In scope of this 'view' method is a variable called $auto_id, which I would like to add to the form.

What would be the best way to achieve this? As I see you cannot make Form_builder re-use an existing form and call its add_field method on it?

I also thought about using a pre-process hook on the form, but the hook method only sees the form as its argument, unfortunately.

Any help is greatly appreciated!

Comments

  • edited May 2016
    Oops, I overlooked the "Kitchen sink" example in the documentation!
    http://docs.getfuelcms.com/modules/forms

    Basically, ended up creating a another method in the controller that sets up the array I used to have in forms.php. Then I create a new form on the spot, and pass it on to the view. No Form_builder involved:

    $form_params = $this->setup_form_params($auto_id); $form = $this->fuel->forms->create('myform', $form_params); $vars['form'] = $form->render();


    EDIT: Well apparently it does not want to work. Using the create() method with the 'myform' slug makes it go to Forms->process($slug) for processing, yet I don't have that form in the database (because of the unknown $auto_id value).

    I thought I'd be able to use the process() method as it could take care of the work for me but it seems it requires an entry in the database. So the kitchen sink example does not work (unless that's why it's called kitchen sink!)
  • edited May 2016
    It sounds like you need a "pre_render" hook or something right? Something that is in the render method that gets called before the actual rendering?

    You could also try changing the config itself using something like (haven't tested this yet):
    $form_config = $this->fuel->forms->config(); $form_config['forms']['my_form']['fields']['my_new_field'] = array('type' => 'hidden', 'value' => $auto_id); $this->fuel->forms->set_config($form_config);
    One thing though with hidden fields is that they are cleaned out of the posted content automatically (so you don't get the return url, anti-spam, etc data saved/emailed) but can be added back in using one of the hooks.
  • edited May 2016
    Yeah, exactly. Something just before rendering.

    The idea of altering the config sounds good! I'll try this, thanks. Would it be possible to alter the field of a form received from the database, though? Obviously loading a form from the model using something along the likes of

    $form = $this->forms->get($form_slug); $from->fields['myField']['value'] = 'newValue';
    does not work, and I get an "Indirect modification of overloaded element of <...> has no effect" error from PHP itself.

    Regarding hidden fields, I added the one hidden field I want to be in the email in this line in the source code. Seems to do the job.
  • edited 4:21PM
    Should it be "$form" instead of "$from". Also, there is no public property on the $form object of "fields". You can use $from->form_fields() to get an array of fields.
  • edited May 2016
    Thanks admin, I tested the form_fields() method. No way to alter the fields using it though as it's a getter. I'm a bit stuck at the moment: the config alteration approach you showed would work great but I wanted to make use of FuelCMS's built-in form processing / validation functionality. It seems that if I use the config approach I have to re-implement these things again?

    EDIT: Using the form builder, I can get the extra value to appear in the form as follows:
    $this->load->library('form_builder'); $form_fields = $form->form_fields(); $form_fields['related_auto_id']['value'] = $id; $this->form_builder->set_fields($form_fields); $vars['form'] = $this->form_builder->render();
    However submitting the form obviously renders it again immediately, instead of showing the "Thank you" message.
  • edited 4:21PM
    There is an add_field and I just pushed a remove_field method you can use on the form object which may help too. Also there is now a pre_render method that gets executed during the rendering before the form_fields() method call to retrieve the fields. The pre_render hook passes the form object itself as the first argument to the hook function so it's possible you could use add_field or remove_field with that.
  • edited June 2016
    Brilliant, thank you so much for the help.
Sign In or Register to comment.