NaveenThally
NaveenThally

Reputation: 1014

Codeigniter Form Validation based on the optional value using config file

I am Doing a CodeIgniter form validation using a form_validation file created in the config folder

ex:

$config = array(
    'signupBasic' => array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required'
        ),
        array(
            'field' => 'firstName',
            'label' => 'First Name',
            'rules' => 'required'
        ),
         array(
            'field' => 'companyName',
            'label' => 'Company Name',
            'rules' => 'required'
        ),
        array(
            'field' => 'addMoreOfficer',
            'label' => 'Add More Officers',
            'rules' => 'callback_addmore_check'
        ),
    )
);

The Problem is I Have some fields which are to be validated only if the checkbox is checked how can i achieve this using the $config array method

Upvotes: 3

Views: 505

Answers (1)

Tpojka
Tpojka

Reputation: 7111

public function checkbox()
{
    $data = array(
        'name'        => 'newsletter',
        'id'          => 'newsletter',
        'value'       => 'accept',
        'checked'     => FALSE,
        'style'       => 'margin:10px',
        );

    echo form_open('test/passingthrough');
    echo form_checkbox($data);
    echo form_submit('mysubmit', 'Submit CheckBox!');
}

public function passingthrough()
{
    $this->form_validation->set_rules('mysubmit', '', 'required');

    if ($this->form_validation->run() == FALSE) {
        redirect('test/checkbox', 'refresh');
    } else {
        echo '<pre>', var_dump($this->input->post('newsletter'));
    }
}

Test this code in your environment. I think you can set condition regarding on value of $this->input->post('newsletter') for your following code.

Upvotes: 1

Related Questions