CodeIgniter Forums
Form Validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: Form Validation (/showthread.php?tid=64875)



Form Validation - coldlamper - 04-06-2016

Won't this return true in the 3.0.6 release when it should return false? 

   $this->load->library('form_validation');
   $inputData = ['inp' => []];
   $this->form_validation->set_data($inputData);

   $this->form_validation->set_rules('inp[]', 'Input field', 'required');

   //Will return true
   $this->form_validation->run();


RE: Form Validation - Narf - 04-06-2016

It will return true, but that's what is supposed to happen - the 'required' rule in that case is applied on any elements that the input array may have.


RE: Form Validation - coldlamper - 04-06-2016

(04-06-2016, 09:23 AM)Narf Wrote: It will return true, but that's what is supposed to happen - the 'required' rule in that case is applied on any elements that the input array may have.

Thanks for the answer.

So how would you require an array that has a least one value without being able to bypass it with an empty array?


RE: Form Validation - Narf - 04-06-2016

Something like this:

$this->form_validation->set_rules('inp', 'Foo', 'required');
$this->form_validation->set_rules('inp[]', 'Bar', 'required');

Though, I don't understand everybody's obsession with making their inputs arrays ...

If you want to receive an undefined count of inputs - you don't need to require at least one.
If one is mandatory and more are optional; then the first is not the same as the optional ones - use two separate inputs.
If you're trying to do 'user[name]' when 'user_name' works perfectly fine, then you're just doing it wrong. Smile


RE: Form Validation - albertleao - 04-06-2016

(04-06-2016, 09:51 AM)Narf Wrote: Something like this:

$this->form_validation->set_rules('inp', 'Foo', 'required');
$this->form_validation->set_rules('inp[]', 'Bar', 'required');

Though, I don't understand everybody's obsession with making their inputs arrays ...

If you want to receive an undefined count of inputs - you don't need to require at least one.
If one is mandatory and more are optional; then the first is not the same as the optional ones - use two separate inputs.
If you're trying to do 'user[name]' when 'user_name' works perfectly fine, then you're just doing it wrong. Smile

Thumbs up. I have seen one too many forms with every input being 'form[user][first_name]'  'form[user][last_name]


RE: Form Validation - coldlamper - 04-07-2016

I agree with not using arrays if you don't have to.  My use case is selecting categories where the user must select 1 or more categories. There are many categories and they change quite frequently.