0

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}
  • UPDATE: I included a hidden field with all the field names that I need to run thru the form validation, it feels dirty, but it seems to be working for now. I would appreciate any suggestions on a better way of doing this. – Brad Apr 6 '15 at 16:06
1

Looks like you're looking for the Forn Validation library that is inherited from CodeIgnitor.

What you seem to be trying to do is set the validation rules at thes same time as setting up the form, whereas what you should do is set up the form as you do, but remove the $missing_field_validation setting as we don't need the validation rules yet! Instead we set up validation in the reciving action...

In your recieving action, you can load the form validator as so :

ee()->load->library('form_validation');

Then you can iterate your $missing_fields/vars array, adding the required rules (any any others), finally running the form_validation to get a nice little boolean for validity. All in, something like this :

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());     
    }

    //validate form, load the validator
    ee()->load->library('form_validation');

    //add the rules
    for($i = 0; $i < count($fields); $i++)
    {
        ee()->form_validation->set_rules(
            $fields[$i]->m_field_id, 
            $fields[$i]->m_field_label, 
            'required'
        );
    }

    //run validation
    if ( ee()form_validation->run() === true ){
        //SUCCESS, Valid action here
    } else {
        //MASSIVE FAIL, Invalid action here
    }

}

Last little note, you may not need to check the XID as you are doing if you use the form validator, as the validator will check this for you anyway (or rather the post router)!

For me, I'd move away from trying to pass your validation rules along with the form, instead as you construct your form from the field array in the first place, use the same field array to drive your setting of the validation rules and play the CI way!

Let us know how you get on (tick the answer box if this is of help to you!) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.