3

我有这些问题,我想在“文本”类型的三种输入形式上设置规则,我的规则是这三个中的至少一个具有值(三个中的任何一个),我不知道如何在 CI 中设置它们,因为它们在 run() 被触发时完全执行,所以你们中的任何人都知道如何设置这些规则以在 CI 中形成验证,请分享你的知识。

4

1 回答 1

4

您可以设置自己的验证函数类型。这里有很好的记录,但摘录如下:

<?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_check正在调用username_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 回答