How does Fuel combine saved data into admin forms?

edited December 2013 in Modules
Hi again!

In a Codeigniter build a while back, I modified Form_builder to be used with it, and to repopulate a form, I came up with a method of either reading in POST data, or retrieving existing db data and applying it to a form array of fields, such that "value" was given appropriate content for each form input (eg empty, POSTed data, saved data).

There must be a similar method in Fuel, which I'd like to use outside of the admin, in a user environment (eg a user profile page). I can't seem to find that process though, and I'm sure it's better than my kludge (which didn't account for every field type). Could it be repurposed?

Comments

  • edited 12:40AM
    Hmm... it sounds like Form_builder would be what you may want to use. In .93 there is a contact controller that shows how to make a contact form and send an email using Form_builder. Is that what you are wanting?
  • edited December 2013
    I took a look at the contact code on github you mentioned.. not quite. Here's what I used in the Codeigniter project I mentioned (it goes on a bit, sorry):

    /* * Use Form_builder class to provide HTML * form for given controller / table (from forms config) * Requires db table result object "item" for edits * Stores form in inherited property "form_data['fields']" * * If POST present, submitted value is used */ function _build_form($item = NULL, $readonly = FALSE) { $mode = (is_object($item) && !$readonly) ? 'update' : 'insert'; $mode = ($readonly) ? NULL : $mode; foreach ($this->form_data['fields'] as $key => $val) { $default = (isset($this->form_data['fields'][$key]['default'])) ? $this->form_data['fields'][$key]['default'] : ''; $input = $this->input->post($key); /* * Determine element value */ if (is_object($item)) { // existing data if ($input === FALSE) { // use data or predefined value $value = isset($item->$key) ? $item->$key : $default; } else // if $input present, use it $value = $input; } else { // insert, either predefined or input $value = !empty($input) ? $input : $default; } $this->form_data['fields'][$key]['value'] = $value; // multiple fields (select, radio) with options if (isset($this->form_data['fields'][$key]['options']) && isset($this->form_data['fields'][$key]['type'])) if (is_array($this->form_data['fields'][$key]['options'])) { foreach ($this->form_data['fields'][$key]['options'] as $okey => $oval) { if ($okey == $value) { $status = in_array($this->form_data['fields'][$key]['type'], array('select', 'enum')) ? 'selected' : 'checked'; $this->form_data['fields'][$key][$status] = TRUE; } } } // checkbox if(isset($this->form_data['fields'][$key]['type'])) { if($this->form_data['fields'][$key]['type'] == 'checkbox') { if($mode == 'update' && $value == $default) $this->form_data['fields'][$key]['checked'] = TRUE; else { if(isset($this->form_data['fields'][$key]['checked'])) { // look for explicit 'insert' keyword if($this->form_data['fields'][$key]['checked'] == 'insert') $this->form_data['fields'][$key]['checked'] = TRUE; } else { $this->form_data['fields'][$key]['checked'] = FALSE;} } } } if (isset($this->form_data['fields'][$key]['after_html'])) $this->form_data['fields'][$key]['after_html'] .= form_error($key); else $this->form_data['fields'][$key]['after_html'] = form_error($key); if ($readonly) $this->form_data['fields'][$key]['readonly'] = TRUE; } }

    It covers the basic fields, but not all the types in Fuel (but my project was pretty simple on that front). So it inherits field data from a parent class, loops over them by type, and assigns POST or supplied db data (from $item) or the default value if neither. Error messages get appended using after_html.

    It's OK for the purpose of the project (maybe it should have been called populate_form()), I just felt it would be lacking in a Fuel environment, and in any case, the admin must use something similar in anyway?
  • edited December 2013
    Ah, now I've read through the form_builder docs again, set_field_values() kinda covers this.

    I've only been using a third of what Form_builder and MY_Model can do, if that! Regarding validation, I can see in the blog module means of adding rules, but how would one set them in one object or array?
Sign In or Register to comment.