• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form Validation

#1
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();
Reply

#2
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.
Reply

#3
(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?
Reply

#4
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
Reply

#5
Thumbs Up 
(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]
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply

#6
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.