Welcome Guest, Not a member yet? Register   Sign In
Question about form validation
#1

[eluser]bonjurkes[/eluser]
Hello,

I used a script that is premade with checkboxes for my own project. But at the source of the script that i found it required for form fields to get validated before submitted.

So here comes the problem, i have like 61 checkboxes on my form, and writing like this :

Code:
$this->form_validation->set_rules('a60', 'a60', 'trim|max_length[255]');

will take so much time and wont look so nice in the code. My all formfield's id stars with "a", so is there a way to use something like "$a. $i" for setting rules for every field ? How can i do that ? Otherwise i have to type like a61, a60, a59 etc.
#2

[eluser]Twisted1919[/eluser]
If you know for sure your number of checkboxes, do a for() loop, no problem with this.
But i would use arrays so instead of <input name="a1" /><input name="a2" /> i would use
<input name="a[]" />, then i wouldn't validate this array at all in the form_validation rules, instead, i would do it after the validation has passed, something like:
Code:
if($this->form_validation->run())
{
   if(isset($_POST['a'])&&is;_array($_POST['a']))
   {
      $a=$_POST['a'];
      $a=$this->security->xss_clean($a);
      foreach($a AS $key=>$value)
      {
          if(empty($value)||strlen($value)>255)
          {
             //trigger error.
          }
      }
   }
   else
   {
     //trigger error: come one, check something...
   }
}
#3

[eluser]plain jane[/eluser]
For validating <input name="a[]" />

we have validation rule like:

Code:
$this->form_validation->set_rules('a[]', 'A', 'required');

Thanks,
ci user




Theme © iAndrew 2016 - Forum software by © MyBB