如果值为空且小于100,则在codeigniter上进行表单验证

时间:2018-12-11 16:33:20

标签: php forms codeigniter validation

如果该值为空且小于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);
    }
}

2 个答案:

答案 0 :(得分:0)

如果您正在使用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();

答案 1 :(得分:0)

我找到了解决方案,使用“或”可以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);
    }