3

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

2
  • You need to pass checkbox value to validation to be able to know fi other fields need to be subject of validation. Commented Mar 24, 2015 at 12:10
  • exactly but how to pass that value to the form_validation file in config folder Example would help Commented Mar 24, 2015 at 12:25

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.