Php 可以同时使用内联验证规则和基于配置文件的验证规则吗?

Php 可以同时使用内联验证规则和基于配置文件的验证规则吗?,php,codeigniter,validation,Php,Codeigniter,Validation,PHP/CodeIgniter 为了设置验证逻辑的表单:“需要一个或两个字段”,我必须像这样使用内联表单验证(源代码为): 但是我有很多其他表单,我更喜欢使用基于配置文件的表单验证 我想不出一个办法让两者共存,我也不想现在就把我所有的规则都放到代码体中去 有什么建议吗 在您的库中创建一个文件,将其命名为MY\u Form\u validation class MY_Form_validation extends CI_Form_validation { public function __c

PHP/CodeIgniter

为了设置验证逻辑的表单:“需要一个或两个字段”,我必须像这样使用内联表单验证(源代码为):

但是我有很多其他表单,我更喜欢使用基于配置文件的表单验证

我想不出一个办法让两者共存,我也不想现在就把我所有的规则都放到代码体中去


有什么建议吗

在您的库中创建一个文件,将其命名为
MY\u Form\u validation

class MY_Form_validation extends CI_Form_validation 
{

public function __construct($rules = array())
{
   parent::__construct($rules);
   $this->CI->lang->load('MY_form_validation');
}


function email_phone($str)
{
  if(!$str)
  {   
    // if POST phone exists validate the phone entries   
    //validation for phone
    return TRUE;
  }else{
    //if no phone was entered
    //check the email
    $email = $this->input->post('email'));

    //use the systems built in validation for the email
    //set your error message here

    return $this->valid_email($email) && $this->required($email);
  }
}
}

//or set the message here
$this->form_validation->set_message('email_phone','Please enter either an email or phone.');
$this->form_validation->set_rules('phone', 'Phone Number', 'email_phone');

您可以在同一应用程序中使用配置文件中的规则集或内联规则

config/form_validation.php

$config = array(
    'ruleset1' => array(
        array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required|trim|alpha'
        ),
    )
);
控制器示例

    public function ruleset()
    {
        if ($this->input->post())
        {
            // runs validation using ruleset1 from the config
            if ($this->form_validation->run('ruleset1') == FALSE)
            {
                ...
            }
        }
    }

    public function inline_rules()
    {
        if ($this->input->post())
        {
            // ignores the config and uses the inline rules
            $this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric');

            if ($this->form_validation->run() == FALSE)
            {
                ...
            }
        }
    }

注意:我发现尝试将它们混合在同一个表单中是行不通的。在同一表单上指定内联规则和规则集将导致完全忽略规则集并应用内联规则。

您是否尝试过使用这两种类型?在上面的代码中,您只使用内联。你打算在同一份表格中同时使用它们还是只在其中一份/或每份表格中使用它们?我打算在同一份申请中同时使用它们,但不是在同一份表格中。任何一种表单都将使用这两种类型中的一种。
    public function ruleset()
    {
        if ($this->input->post())
        {
            // runs validation using ruleset1 from the config
            if ($this->form_validation->run('ruleset1') == FALSE)
            {
                ...
            }
        }
    }

    public function inline_rules()
    {
        if ($this->input->post())
        {
            // ignores the config and uses the inline rules
            $this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric');

            if ($this->form_validation->run() == FALSE)
            {
                ...
            }
        }
    }