【问题标题】:form validation on codeigniter if the value is empty and below 100如果值为空且低于 100,则在 codeigniter 上进行表单验证
【发布时间】:2018-12-11 16:33:19
【问题描述】:

如果值为空且低于 100,如何在 codeigniter 上验证我的表单。

这是我的控制器

function aksi_deposit(){
    if ((empty($_POST['deposit']))&& (($_POST['deposit'])<100)) {
        redirect('deposit');
    } else {
        $whc = $this->input->post('deposit');
        $money = $whc * 8000;
        $idUser= 1;
        $b['data'] = $this->m_user->tampil_invoice($idUser);
        $b['coin'] = $money;
        $b['whc'] = $whc;
        $this->load->view('user/v_invoice',$b);
    }
}

【问题讨论】:

  • 当然,如果某个东西是空的,它低于 100,你的意思是空的还是低于 100。
  • 是的。使用“或”这项工作 100%。谢谢。代码是: if ((empty($_POST['deposit']))OR (($_POST['deposit'])
  • 诚实的问题.. 如果您使用的是 codeigniter,为什么不利用 CI 的内置表单验证方法?

标签: php forms codeigniter validation


【解决方案1】:

我找到了解决方案,使用“OR”它可以 100% 工作。

if ((empty($_POST['deposit']))OR (($_POST['deposit'])<100)) {
        redirect('deposit');
    } else {
        $whc = $this->input->post('deposit');
        $money = $whc * 8000;
        $idUser= 1;
        $b['data'] = $this->m_user->tampil_invoice($idUser);
        $b['coin'] = $money;
        $b['whc'] = $whc;
        $this->load->view('user/v_invoice',$b);
    }

【讨论】:

    【解决方案2】:

    如果您使用的是 codeigniter,您应该受益于其内置的表单验证库,它将为您完美处理所有事情,并且非常易于使用,您只需像这样加载它:

    $this->load->library('form_validation');
    $config = array(
        'field'     => 'deposite',
        'label'     => 'deposite',
        'rules' => array('trim','required', array('input_less_than_100',
            function($input)
            {
                return ( $input < 100 ) ? FALSE : TRUE;
            }),
        ),
        'errors' => array(
            'input_less_than_100' => 'The %s field is less than 100.',
        ),
    );
    $this->form_validation->set_rules($config);
    

    然后像这样运行您的验证:

    if ($this->form_validation->run() === TRUE)
    {
        // do your magic
    }
    else
    {
        // redirect if you want and then show form validation errors
    }
    

    您可以使用这样的验证错误:

    $this->form_validation->error_array();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-03
      • 2016-04-18
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多