3

タイプ「テキスト」の3つの入力フォームにルールを設定したいという問題があります。私のルールは、これら3つのうち少なくとも1つに値(3つのうちのいずれか)があるということです。CIでそれらを設定する方法がわかりませんこれらは run() がトリガーされたときに完全に実行されるため、これらの種類のルールを CI でフォーム検証に設定する方法を知っている人は、知識を共有してください。

4

1 に答える 1

4

独自の種類の検証関数を設定できます。hereで十分に文書化されていますが、抜粋は次のとおりです。

<?php

class Form extends CI_Controller {

    public function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }

    public function username_check($str)
    {
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

}
?>

callback_username_checkusername_checkコントローラーで関数を呼び出しています

最新のコメントに答えるには

// $data is $_POST 
function my_form_validator($data)
{
    $data = 'dont worry about this';
    // you have access to $_POST here
    $field1 = $_POST['field1'];


    if($field1 OR $field2 OR $field3)
    {
        // your fields have value       
        return TRUE;
    }
    else
    {
        // your fields dont have any value
        $this->form_validation->set_message('field1', 'At least one of the 3 fields should have a value');

        return FALSE;
    }
}
于 2013-03-24T12:38:08.777 に答える