Forms module how to return form values in form

edited February 2017 in Modules
Cant for the life of me figure this one out. When form created in form module is submitted. Then validates content if there is an error returns the form showing error. But all fields that where ok and filled in are not being remembered. How do i add the value back into the form on validation. is there something i need to add to the block template so that values are returned in the fields. i currently use $field['label] and $field['field'] to display in block template. This is of course when the form has been submitted and the server side validation kicks in.

Comments

  • edited 4:37PM
    In your form block, are you creating the form fields or relying on the Forms module to generate the form? If you are creating your own fields, the fuel/modules/forms/controllers/Forms.php controller sets a "posted" flash data variable that can be accessed through the $CI->session->flashdata('my_var');
  • edited 4:37PM
    Hi i use the form module to create the form, but use a block template for designing placing fields where i want them. So i cant see how i can put the values back into the form after submission
  • edited 4:37PM
    The fuel/modules/forms/libraries/Fuel_forms.php file has a method in it called rendered_vars around line 1215. I believe a change to that method may help you out as opposed to adding a call to the session library in your forms.php config to set the value of your field from the session. Note the added line to grab the $posted array from the flashdata and then a check to set the $form_field['value'] in the loop:
    public function rendered_vars($form_fields) { $rendered_fields = array(); $vars = array(); $posted = ($this->CI->session->flashdata('posted')) ? (array) $this->CI->session->flashdata('posted') : $_POST; foreach($form_fields as $key => $form_field) { if (isset($posted[$key])) { $form_field['value'] = $posted[$key]; } $rendered_fields[$key]['field'] = $this->CI->form_builder->create_field($form_field); $rendered_fields[$key]['label'] = $this->CI->form_builder->create_label($form_field); $rendered_fields[$key]['key'] = $key; $vars[$key.'_field'] = $rendered_fields[$key]['field']; $vars[$key.'_label'] = $rendered_fields[$key]['label']; } $vars['fields'] = $rendered_fields; $vars['form'] = $this; return $vars; }

    Let me know if that fixes your issue and I can add it to the repo.
  • edited 4:37PM
    Thanks for this will let you know how it goes as soon as i implement this into our system
  • edited March 2017
    Hi sorry for not getting back sooner have tried the above code and slightly modified but works at treat. There was one thing i could not get anything to appear when i tried adding errors to $form_field['after_label'] but apart from that all good

    public function rendered_vars($form_fields)
    {

    $rendered_fields = array();
    $vars = array();
    $posted = ($this->CI->session->flashdata('post')) ? (array) $this->CI->session->flashdata('post') : $_POST;
    $errors = ($this->CI->session->flashdata('error')) ? (array) $this->CI->session->flashdata('error') : '';
    foreach($form_fields as $key => $form_field)
    {
    if(isset($errors[$key])){
    $form_field['before_html'] = '
    '.$errors[$key].'
    ';
    }

    if (isset($posted[$key]))
    {
    $form_field['value'] = $posted[$key];
    } else {
    $form_field['value'] = $this->CI->input->post($key);
    }

    $rendered_fields[$key]['field'] = $this->CI->form_builder->create_field($form_field);
    $rendered_fields[$key]['label'] = $this->CI->form_builder->create_label($form_field);
    $rendered_fields[$key]['key'] = $key;
    $vars[$key.'_field'] = $rendered_fields[$key]['field'];
    $vars[$key.'_label'] = $rendered_fields[$key]['label'];
    }
    $vars['fields'] = $rendered_fields;
    $vars['form'] = $this;
    return $vars;
    }
Sign In or Register to comment.