【问题标题】:Can one use inline validation rules, and config file based validation rules simultaneously?可以同时使用内联验证规则和基于配置文件的验证规则吗?
【发布时间】:2014-02-20 01:38:31
【问题描述】:

PHP / CodeIgniter.

为了设置一个验证逻辑的表单:“需要一个或两个字段”我必须像这样使用内联表单验证(来源是http://ellislab.com/forums/viewthread/136417/#672903):

if ( ! $this->input->post('email'))
{
   $this->form_validation->set_rules('phone', 'Phone Number', 'required');
}
else
{
   $this->form_validation->set_rules('phone', 'Phone Number', '');
}

// If no phone number, email is required
if ( ! $this->input->post('phone'))
{
   $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
}
else
{
   $this->form_validation->set_rules('email', 'Email Address', 'valid_email');
} 

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

我想不出让这两者共存的方法,而且我现在真的不想把我所有的规则都带入代码体中。

有什么建议吗?

【问题讨论】:

  • 您是否尝试过使用这两种类型?在上面的代码中,您只使用内联。您打算将它们一起用于同一个表单还是仅一个/或每个表单?
  • 我打算在同一个应用程序中一起使用它们,但不是同一种形式。任何一种形式都将使用这两种类型中的一种。

标签: php codeigniter validation


【解决方案1】:

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

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)
            {
                ...
            }
        }
    }

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

【讨论】:

    【解决方案2】:

    在您的库中创建一个文件,将其命名为 MY_Form_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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      相关资源
      最近更新 更多