Form builder - set_field_values(...)

edited February 2014 in Share
I try ti use form builder for frontend.
I retrieve record from model using something like this:
$record = $this->{base_module_model}->find_one_by_id_and_user_id($id,$user->id);
The model has 'has_many' relationships and serialized_fields and I want to populate them in the from with all values.
In the admin everything is all right.
How the magic is happen?
I set the form builder class and try many methods but can't retrieve the right array to set field values...
//get record from classes (base modeule model) $record= $this->classes_model->find_one_by_id_and_user_id($id,$user->id); $this->load->module_library('classy','bootstrap_form_builder'); //extend form-builder $fields = $this->classes_model->form_fields(); $this->bootstrap_form_builder->set_fields($fields); $this->bootstrap_form_builder->set_field_values($record->values());// $record->values() work but without the relationship and serialzed_fields? Can'find how this happen in admin... ....

Comments

  • edited 8:40AM
    Try the following instead of $record->values():
    $this->classes_model->find_one_array(array('id' => $id));
  • edited 8:40AM
    I've tried this, but the result array has no relationship values, only the values from the classes model table!
  • edited 8:40AM
    Maybe must retrieve relationship values with another method... on_after_get or something and merge the results?
  • edited 8:40AM
    On line 2762 in MY_Model, the values for the has_many relationship should be getting set.
    $related_vals = ( ! empty($values['id'])) ? $this->get_related_keys($values, $related_model, 'has_many', $rel_config) : array();
    If you output $related_vals, is there any value?
  • edited 8:40AM
    Yes!!! Working like a charm!
  • edited 8:40AM
    So does this mean that $related_vals variable is getting set correctly but you are still having a problem with the values being set in the form? If so, are those values in the $values parameter of your model's form_fields method (the first parameter passed to the form_fields method)?
  • edited 8:40AM
    Yes. :(... I did something like this:
    $values = $this->classes_model->find_one_array(array('id' => $id)); $fields = $this->classes_model->form_fields($values); var_dump($fields);// return all filelds but only values of the related are setted. foreach($fields as $key=>$field){ $field['value'] = $values[$key]; } var_dump($fields); // now all are getting set correctly... but it's not so good looking :-)
  • edited 8:40AM
    Maybe I miss something... I don't want to waste your time, but if you have willingness to help to do it in the right way I'll be glad.
    In the above example the solution is working for me, because I need this loop to set some other props for the fields, but think that there is more elegant way...
    Thanks for your answers. FUEL is great job.
  • edited 8:40AM
    It is possible that some of the values that you are needing to be set in the form aren't getting passed in the first $values parameter. By default, simple modules have an "edit_method" property you can set on them. By default, it uses the find_one_array method (as stated earlier), however, you can change that to be a new method on your model, or you can simply overwrite your find_one_array method to return the values that you need to set the value attributes of your form fields. The setting of the values for the related happens in the form_fields method (which is what you are seeing... and as you probably saw when debugging the issue in the MY_Model class). The set_field_values method should fill out the rest of the field values as long as you aren't overwriting it with an empty value when passing it to the set_field_values method or something.
Sign In or Register to comment.