How to validate my form on codeigniter, if the value is empty and below 100.
this is my controller
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);
}
}
i've found the solution, using "OR" it works 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);
}
If you are using codeigniter you should benefit from its built-in form validation library it will handle everything perfectly for you and it is extremely easy to use, you will just load it like this:
$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);
Then run your validation like this:
if ($this->form_validation->run() === TRUE)
{
// do your magic
}
else
{
// redirect if you want and then show form validation errors
}
You can use then validation errors like this:
$this->form_validation->error_array();