Form in Backend and Frontend

I build an advanced module with an extensive form in the backend that I want to display partially in the frontend (User register in the frontend and admin approves and changes in the backend.)

I setup my model in the backend and now I attempt to load the model in the frontend

/modules/partners/model/Partner_model.php

class Partner_model extends Base_module_model {
    function form_fields($values = array(), $related = array())
    {
        $fields = parent::form_fields($values, $related);

        $this->form_builder->set_params([
            'exclude' => [
                'field_to_exclude'
            ]
        ]);

        // do something to the fields

        return $fields;
        }
}

Then I load my model in the frontend, to have access to all the form fields from the database
/modules/partners/controllers/Partner.php

<?php
class Partner extends CI_Controller {

    var $partner_config;

    function __construct()
    {
        parent::__construct();
    }

    /**
     * Submit a new Partner Entry
     * @return void
     */
    function submit()
    {
        $this->load->model('partner_model');        

        $this->load->library('form_builder');

        $this->form_builder->initialize(array(
            'form_attrs' => array(
                'method' => 'post',
                'action' => '/partner/submit'
            ),
            // different exclude setting 
            'exclude' => ['other_field_to_exclude','foo']
        ));


        $fields = $this->partner_model->form_fields();

        $this->form_builder->set_fields($fields);

        // excluded fields are ignored instead it hast the settings of partner_model.excluded
        $this->form_builder->render(),


    }
}

But I can't set exclude fields here in the submit() method.

Any idea?

Comments

  • Got it, I need to load

    $this->load->model('partner_items_model');
    $fields = $this->partner_items_model->form_fields();
    

    before $this->form_builder->initialize

  • edited November 2018

    What is best practice to share a model with frontend? reusing all the hooks etc? Is that possible without problems? What if I use $this->fuel->admin->set_notification in the hook? That wouldn't work in the frontend?!

  • I'm just going to talk to myself here, sometimes it helps writing things out :). If someone can help, that would be great!

    /**
     * Submit a new Entry
     * @return void
     */
    function submit()
    {
        // needed for the flash data
        $this->load->library('session');
        $this->load->library('form_builder');
        $this->load->library('MY_Form_builder'); // extended Formbuilder to do some extra stuff
        $this->my_form_builder->load_custom_fields(APPPATH.'config/custom_fields.php');
    
        $this->load->model('partner_items_model');
    
        if (!empty($_POST))
        {
            // getting my models validation object
            // [1]
            $validator = $this->partner_items_model->get_validation();
    
            // add some extra rules for frontend
            $validator->add_rule('cat', 'required', "Das Feld Kategorie muss ausgewählt werden", array($this->input->post('an_kategorie')));
    
            $validator->validate($this->input->post());
    
            // save
            $saved = null;
            if($validator->is_valid()){
    
                // calculate some stuff in the models hook
                #$_POST = $this->partner_items_model->on_before_clean($_POST);
    
                // what else is recommended here? Sanitize the input?
    
                // save entry to db
                // [2]
                $saved = $this->partner_items_model->save($this->input->post());
    
                if($saved){
                    // send Partnerobject see Blog.php:345
                    // create new entry
                    // TODO: send E-Mail to Admin and evt. user
                    $notified = $this->_notify($partner);
                }
    
                // TODO: Show success Message
                // [3]
            }
            // TODO: Show errors, how?
            [4]
            //d($this->input->post(),$saved,$this->partner_items_model->get_errors());
    
        }
    
        $exclude = [];
    
        // load fields first
        $fields = $this->partner_items_model->form_fields();
        // exclude some fields and sections
        $fields = array_diff_key($fields, array_flip($exclude));
    
        // then init form_builder and overwrite exclude fields etc
        $this->my_form_builder->initialize(array(
            'form_attrs' => array(
                'method' => 'post',
                'action' => '/partner/submit'
            ),
            'no_css_js' => TRUE, // don't load Fuel backend JS/CSS
            //'js' => $js,
            'submit_value' => '<button type="submit" name="submit_form" class="btn btn-info">Eintrag absenden</button>',
            'textarea_rows' => '5',
            'textarea_cols' => '28',
            'exclude' => $exclude,
    
            'display_errors' => TRUE,
    
            // Render Form Options
            'render_format' => 'divs',
            'label_layout' => 'left', //top|left
            // only in my_form_builder
            'bootstrap_field_col_classes' => 'col-sm-7',
            'bootstrap_label_col_classes' => 'col-sm-3 col-form-label'
    
        ));
    
        $this->my_form_builder->set_fields($fields);
        $this->my_form_builder->set_field_values($this->input->post());
    
        // render und process form.
    
        $vars = [
            'form' => $this->my_form_builder->render_bootstrap_divs(),
            'layout' => 'kiosk', // !needed
            'page_title' => 'Partner senden',
        ];
    
        // @link https://forum.getfuelcms.com/discussion/3057/advanced-module-and-layout-file/
        // @link http://docs.getfuelcms.com/libraries/fuel_pages
        $output = $this->fuel->pages->render('submit', $vars,[
            'view_module' => PARTNER_FOLDER,
            'language' => 'de'
        ], True);
    
        $this->output->set_output($output);
    
    }
    

    [1] How can I see what validation rules from the model are applied? (http://docs.getfuelcms.com/libraries/my_model) => auto_validate, auto_validate_fields, required. How can I use as much FUEL methods as possible?

    [2] Or are all validation rules from the model are applied in the model->save() method? How can I check which rules are applied here?

    [3] Do I have access on the Fuel success message? (Which is used in the backend)

    [4] What about errors? Are they automatically attached to the rendered form from the form_builder or do I have to assign them to my template myself?

    I looked up the frontend stuff from the blog module, because this comes the closest to what I want to do (send/save a form), but there is so much back and forth in the code, that it is hard to follow. And sometimes the docs just miss proper examples.

    Please advise how to proceed from here.
    Thanks so much!

  • 1 & 2. In the model's on_before_save hook, try adding the following and click save:

    echo '<pre>';
    print_r($model->get_validation()->fields());
    echo '</pre>';
    exit();
    
    1. The fuel success message is in the fuel/modules/fuel/language/english/fuel_lang.php file and I believe can be accessed and altered as so CI()->lang->language['data_saved'] = 'New Success Message'; You can call it with lang('data_saved'). It's using a CI session flash notification to render and you will need to add that code to your controller.
    2. Errors should automatically get set on the Global errors object which the Form_builder object will use by default.
Sign In or Register to comment.