Form_builder js in Fuel admin

edited November 2015 in Share
I have a situation where I'm creating dynamic inputs in a simple module in Fuel's admin, and using the on_after_save() method to pick up the POST parameters and process them.

If the values pre-exist in relation to the form record, then form_fields() is rendering them from the model. If new inputs are required, a js Controller is creating them, using a button's click event handler. This all works OK.

However, I don't like repeating 2 lots of identical form input HTML in the Controller js and in PHP (the model), so it seems a good idea to get this HTML generation for new elements all done in the model, and by form_fields(). It will still require javascript, but the script could be defined in the model.

I can of course add javascript via a Form builder input, but is there a way, from form_fields(), of adding or appending to the form's js property? The functionality seems relevant to the form as a whole, rather than being loaded arbitrarily by a given input field.


  • edited 1:59AM
    I've come to a halfway solution: create a "section" field, which is not an input ("fieldset" would have done but for the enclosure) and use the js key of that to define the function I need, rather than placing it in the Controller.js. Now I can define the "edit" HTML from form_fields() and output a javascript from the model too. All the dynamic HTML is in one place now (the model) but - careful what you wish for, I had to escape single and double quotes like crazy to embed javascript in PHP!!
  • edited 1:59AM
    There is also a special field you can specify with a key of "__FORM_BUILDER__" which will pass configuration parameters to the Form_builder object. In your case you could do something like the following in your form_fields method:
    $fields['__FORM_BUILDER__'] = array('js' => '<script>alert("YO");</script>');
  • edited 1:59AM
    Another secret recipe to consider!

    Thank you.
Sign In or Register to comment.