Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow empty or numeric values in codeigniter's form_validation class?

I'd like to have input validated with form_validation class, that'll allow me to put numeric or empty value in the field.
Something like this:

$this->form_validation->set_rules('field[]','The field','numeric or empty|xss_clean');

Is this possible to achieve?

like image 725
sunpietro Avatar asked Jan 29 '26 07:01

sunpietro


2 Answers

$this->form_validation->set_rules('field[]', 'The field', 'numeric|xss_clean');

This should be sufficient in theory, since the field hasn't been set to required.

like image 167
Sam Granger Avatar answered Jan 30 '26 22:01

Sam Granger


But, it returns me the errors

Then perhaps an extra step:

if (!empty($this->input->post('field[]')))
{
    $this->form_validation->set_rules('field[]', 'The field', 'numeric|xss_clean');
}
like image 31
stealthyninja Avatar answered Jan 30 '26 22:01

stealthyninja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!