Form Validation |
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();
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.
(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?
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.
(04-06-2016, 09:51 AM)Narf Wrote: Something like this: 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!
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.
|
Welcome Guest, Not a member yet? Register Sign In |