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 に答える