jayer
jayer

Reputation: 49

How can I write a conditional for set_rules() with form validation in CodeIgniter?

I have 3 fields in my form - lets say A, B, and C. I want to set the validation rules to where if fields A and B are empty then require C. Otherwise, require A and B.

I looked up some material on this and basically I found that I can use a callback function, but I'm a little new to CodeIgniter and I can't quite figure out the syntax to write this out.

Upvotes: 4

Views: 8383

Answers (3)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

this is simple

function index()
{
    $this->load->helper(array('form', 'url'));

    $this->load->library('form_validation');

    $post_data  =   $this->input->post();

    $this->form_validation->set_rules('A', 'FieldA', 'required');
    $this->form_validation->set_rules('B', 'FieldB', 'required');

    if(!isset($post_data['A']) AND !isset($post_data['B']))
    {
        $this->form_validation->set_rules('C', 'FieldC', 'required');
    }   


    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('success');
    }
}

Upvotes: 4

Manish Kothari
Manish Kothari

Reputation: 1712

You can do it this way as shown below, if you place the set_rules in an if construct you may have problems when you are trying to repopulate using form helpers.

function index()
{
    $required='';
    if(isset($this->input->post('A')) && isset($this->input->post('B')))
    {
        $required='required';
    }
    $this->form_validation->set_rules('A', 'FieldA', 'required');
    $this->form_validation->set_rules('B', 'FieldB', 'required');
    $this->form_validation->set_rules('C', 'FieldC', $required);

    if ($this->form_validation->run() == FALSE)
    {
         $this->load->view('myform');
    }
    else
    {
        $this->load->view('success');
    }
}

Upvotes: 1

WoMo
WoMo

Reputation: 7246

A callback is the cleanest way to handle this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class YourController extends CI_Controller {

    public function save() 
    {
        //.... Your controller method called on submit

        $this->load->library('form_validation');

        // Build validation rules array
        $validation_rules = array(
                                array(
                                    'field' => 'A',
                                    'label' => 'Field A',
                                    'rules' => 'trim|xss_clean'
                                    ),
                                array(
                                    'field' => 'B',
                                    'label' => 'Field B',
                                    'rules' => 'trim|xss_clean'
                                    ),
                                array(
                                    'field' => 'C',
                                    'label' => 'Field C',
                                    'rules' => 'trim|xss_clean|callback_required_inputs'
                                    )
                                );
        $this->form_validation->set_rules($validation_rules);
        $valid = $this->form_validation->run();

        // Handle $valid success (true) or failure (false)

    }


    public function required_inputs()
    {
        if( ! $this->input->post('A') AND ! $this->input->post('B') AND $this->input->post('C'))
        {
            $this->form_validation->set_message('required_inputs', 'Either A and B are required, or C.');
            return FALSE;
        }

        return TRUE;
    }
}

Upvotes: 7

Related Questions