Forms module how to return form values in form
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
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.
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'] = '
}
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;
}