form_fields values in advanced module

edited September 2016 in Modules
Hi admin,

I've created advanced module for messages. I'm trying to retrieve form fileds, but when form is rendered, fields return with no values. so, please find below a short of my code and fields returned:

In controller:

function edit($id = NULL){
$this->load->library('form_builder');

$vars['instructions'] = 'Here instructions';
$values = (array)$this->messages_model->get_message($id)[0];
$fields = $this->messages_model->form_fields($values);

$this->form_builder->load_custom_fields(APPPATH.'config/custom_fields.php');
$this->form_builder->submit_value = lang('btn_upload');
$this->form_builder->set_fields($fields);

$vars['form'] = $this->form_builder->render();
$this->fuel->admin->render('_blocks/module_create_edit', $vars, '', MESSAGES_FOLDER);
}
For above code, $id is id for mySql table and $values is loaded with :

Array
(
[id] => 1
[date_added] => 2016-09-25
[last_modified] => 2016-09-26 19:12:52
[title_message] => titulo mensaje
[body_message] => El mensaje que se desea enviar
[file] =>
[all_operators] => 0
[operators_list] => ["4","15","1","2","7","9","16","17"]
[deleted] => 0
)
In model:

function form_fields($values = array()){
$fields = parent::form_fields($values);
return $fields;
}
Array $fields returned with no values:
Array
(
[id] => Array
(
[name] => id
[type] => int
[default] =>
[options] =>
[max_length] => 10
[primary_key] => 1
[comment] =>
[collation] =>
[extra] => auto_increment
[null] =>
[required] =>
)

[date_added] => Array
(
[name] => date_added
[type] => hidden
[default] =>
[options] =>
[max_length] =>
[primary_key] =>
[comment] =>
[collation] =>
[extra] =>
[null] => 1
[required] =>
)

[last_modified] => Array
(
[name] => last_modified
[type] => hidden
[default] => CURRENT_TIMESTAMP
[options] =>
[max_length] =>
[primary_key] =>
[comment] =>
[collation] =>
[extra] =>
[null] => 1
[required] =>
)

[title_message] => Array
(
[name] => title_message
[type] => string
[default] =>
[options] =>
[max_length] => 100
[primary_key] =>
[comment] =>
[collation] => latin1_swedish_ci
[extra] =>
[null] => 1
[required] => 1
)
...
...

What is your advise for above code? I'm forget something ?

Comments

  • edited 6:15PM
    Add the following line:
    $this->form_builder->set_field_values($values);
  • edited 6:15PM
    Great, works fine, thank you !
  • edited 6:15PM
    Continuing with the form, I have a 2 new questions:
    1. How can I set attrs into form tag, I tried with:

    $this->form_builder->set_fields($fields);
    $this->form_builder->form_attrs = 'method="post" action="some/url" enctype="multipart/form-data"';
    $this->form_builder->set_field_values($values);
    But it doesn't work.

    2. When send javascript from controller to view, It seems no be rendered, or not printed
    In controller:

    $vars['js'] = '<script>alert("Message")</script>'
    In view:
    echo $js;
  • edited 6:15PM
    1. What is appearing instead and how are you displaying it in the view file?
    2. How are you passing the $vars to the view?
  • edited October 2016
    In admin form dashboard appears:
    1. <form action="http://mysite.com/dashboard/messages/edit/1?inline="; method="post"; id="form"; enctype="multipart/form-data">...
    In view I use:
    <?php echo $form; ?>
    In controler:
    $vars['form'] = $this->form_builder->render();
    $this->fuel->admin->render('_blocks/module_create_edit', $vars, '', MESSAGES_FOLDER);
    2. $this->fuel->admin->render('_blocks/module_create_edit', $vars, '', MESSAGES_FOLDER);
    In item 2, problem solved removing a typo.
  • edited 6:15PM
    If you are using the main admin template, the form tag may already be in there so check to see if you actually have a form within a form already (which isn't valid HTML). You can pass the variable 'form_action' if you want to set a different action and remove the form tags of your form with:
    $this->form_builder->use_form_tag = FALSE;
  • edited 6:15PM
    Unfortunately it doesn't work, I have only one form and put:

    $this->form_builder->use_form_tag = FALSE;
    $this->form_builder->form_action = 'some/action';
    // $this->form_builder->form_attrs = array ('method' =>'post', 'action' =>'some/url', 'enctype' =>'multipart/form-data');
    And result is the same:

    <form action="http://mysite.com/dashboard/messages/edit/1?inline="; method="post" id="form" enctype="multipart/form-data">
    So, is there some method for remove ?inline get variable ?
  • edited 6:15PM
    Did you try setting the variable "form_action" to get passed to the view?
Sign In or Register to comment.