【问题标题】:Codeigniter form validation - at least one field is requiredCodeigniter 表单验证 - 至少需要一个字段
【发布时间】:2015-05-20 19:01:38
【问题描述】:

我有一个包含 3 个字段的提交表单。提交前应至少填写其中一项。你能帮我用codeigniter表单验证来做吗?

控制器

$this->form_validation->set_rules('country', 'Country','trim|strip_tags');
$this->form_validation->set_rules('state','State','trim|strip_tags');
$this->form_validation->set_rules('city','City','trimstrip_tags');

【问题讨论】:

  • 我认为你应该扩展验证类
  • 如果您想在提交前进行验证,您应该在Javascript / jquery 中进行。服务器端验证将在发布表单后起作用,使用此规则您可以验证 $this->form_validation->set_rules('country', 'Country','required');

标签: php codeigniter


【解决方案1】:
$this->form_validation->set_rules('country','Country','trim|strip_tags|callback_validate_either');
$this->form_validation->set_rules('state','State','trim|strip_tags|callback_validate_either');
$this->form_validation->set_rules('city','City','trimstrip_tags|callback_validate_either');

添加回调验证函数

function validate_either(){
    if($this->input->post('country') || $this->input->post('state') || $this->input->post('city')){
        return TRUE;
    }else{
        $this->form_validation->set_message('validate_either', 'Please enter atleast one of City , State or Country');
        return FALSE;
    }
}

根据 Codeigniter 文档,错误消息是通过传递方法名称来设置的(不带“callback_”前缀):

【讨论】:

    【解决方案2】:

    不是大师,但这将是我的方法......

    function do_add()
    {
       if( (isset($_POST['country']) || (isset($_POST['state']) || (isset($_POST['city']) )
       {
          //Do stuff
       }
       else
       {
          //Redirect or whatever
       }
    }
    

    【讨论】:

    • 在您的代码中只需要城市字段,我需要至少一个字段
    • @EducateYourself 好的,我已经更改了我的代码。这是你想要的吗?如果设置了$_POST 中的以下值之一,则您可以通过 alpha/trim/etc 的验证规则。
    【解决方案3】:

    如果复制代码,则第三条规则中缺少一个管道。此外,您可以将 required 规则设置为每个规则的第一条规则。

    $this->form_validation->set_rules('country', 'Country','required|trim|strip_tags');
    $this->form_validation->set_rules('state','State','required|trim|strip_tags');
    $this->form_validation->set_rules('city','City','required|trim|strip_tags');
    

    CI Rule Reference.

    【讨论】:

      【解决方案4】:

      您可能需要使用自定义验证

      $this->form_validation->set_rules('country', 'Country','trim|strip_tags|callback_validate_either');
      $this->form_validation->set_rules('state','State','trim|strip_tags|callback_validate_either');
      $this->form_validation->set_rules('city','City','trimstrip_tags|callback_validate_either');
      

      然后添加验证功能

      function validate_either(){
             if($this->input->post('country') || $this->input->post('state') || $this->input->post('city')){
                  return TRUE;
             }else{
                  $this->form_validation->set_message('validate_name', 'Please enter atleast one of City , State or Country');
              return FALSE;
             }
          }
      

      【讨论】:

        猜你喜欢
        • 2011-07-21
        • 2014-06-17
        • 2017-06-02
        • 2017-07-05
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 2018-04-01
        相关资源
        最近更新 更多