4

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

我查阅了一些关于这个的材料,基本上我发现我可以使用回调函数,但我对 CodeIgniter 有点陌生,我不太清楚写出来的语法。

4

3 回答 3

7

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

<?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;
    }
}
于 2013-01-22T18:27:41.487 回答
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');
    }
}
于 2013-01-22T18:19:57.300 回答
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');
    }
}
于 2013-09-01T03:58:47.870 回答