I am creating a dynamic form on the fly for use in EE templates. I create the tags using the mod.module_name.php file of my module.
I am trying to run validation on the form, every field is required so I have a separate array in the loop that assigns the fields to template tags, to be returned to the template for the user to use those template tags to construct the form in their template.
I have a separate function in the mod.module_name.php file that I have registered as an action in the actions table for it to process/post that form to that function. I tried to store the form validation in a session to check to make sure all those fields have a value in it, but it does not allow me to store that in session.
Below is my update_form function that will display opening and closing form tag and within it will be the form fields
public function update_form()
{
$action = $this->EE->functions->fetch_site_index(0, 0).QUERY_MARKER.'ACT='.$this->EE->functions->fetch_action_id($this->classname, 'process_update_form');
$form_details = array(
'action' => $action,
'secure' => TRUE,
'id' => $this->EE->TMPL->fetch_param('form_id', 'profile_update_form'),
'class' => $this->EE->TMPL->fetch_param('form_class', ''),
);
$out = $this->EE->functions->form_declaration($form_details);
for($i = 0; $i < count($missing_fields); $i++)
{
// this array will be used for setting the form validation rules
$missing_field_validation[] = array(
'field' => 'm_field_id_ ' . $missing_fields[$i]->m_field_id,
'label' => $missing_fields[$i]->m_field_label,
'rules' => 'required',
);
$vars = array(
'field_id' => $missing_fields[$i]->m_field_id,
'field_name' => $missing_fields[$i]->m_field_name,
'field_label' => $missing_fields[$i]->m_field_label,
'field_type' => $missing_fields[$i]->m_field_type,
'field_list_items' => format_list_items($missing_fields[$i]->m_field_list_items),
'count' => $i+1,
'total_fields' => $total_missing_fields,
);
$out .= $this->EE->TMPL->parse_variables_row($this->EE->TMPL->tagdata, $vars);
}
return $out;
}
Below is the form that is triggered (it is registered in the actions table), I need to set the form_validation with the $missing_field_validation array without the need of using front-end validation.
public function process_update_form()
{
// get form data and update it
if ($this->EE->security->check_xid($this->EE->input->post('XID')) == FALSE)
{
// No data insertion if a hash isn't found or is too old
$this->EE->functions->redirect($this->EE->functions->form_backtrack());
}
}
Template tags, just in case it helps you understand what I am trying to accomplish:
{exp:profile_reminder:update_form}
<p>{field_id}</p>
<p>{field_name}</p>
<p>{field_label}</p>
<p>{field_type}</p>
<p>Total: {total_fields}</p>
{if count==total_fields}
<input type="submit">
{/if}
{/exp:profile_reminder:update_form}