如何在CodeIgniter中使用表单验证为set_rules()编写条件?

时间:2013-01-22 17:32:54

标签: codeigniter

我的表单中有3个字段 - 让我们说A,B和C.我想将验证规则设置为如果字段A和B为空则需要C.否则,需要A和B.

我查了一些这方面的资料,基本上我发现我可以使用回调函数,但我对CodeIgniter有点新意,我无法弄清楚要写出来的语法。

3 个答案:

答案 0 :(得分:6)

回调是处理此问题的最简洁方法:

<?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;
    }
}

答案 1 :(得分:4)

这很简单

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

答案 2 :(得分:1)

你可以这样做,如下所示,如果你将set_rules放在if结构中,当你尝试使用表单助手重新填充时可能会遇到问题。

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