CodeIgniter Forums
form_validation with assoc arrays - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: form_validation with assoc arrays (/showthread.php?tid=19223)



form_validation with assoc arrays - El Forum - 06-01-2009

[eluser]MDomansky[/eluser]
Hi,

I try to validate array like this
Code:
array
'specification' =>
    array
      1 =>
        array
          'name' => string '3/часть Crawford, B34534A34543/324234234' (length=45)
          'fullName' => string '3/часть' (length=12)
          'count' => string '3' (length=1)
          'unit' => string 'THING' (length=5)
          'amount' => string '577' (length=3)
          'currency' => string 'USD' (length=3)
          'action' => string 'update' (length=6)
      2 =>
        array
          'name' => string '2/часть Crawford, B4354A435435435/34435435' (length=47)
          'fullName' => string '2/часть' (length=12)
          'count' => string '10' (length=2)
          'unit' => string 'THING' (length=5)
          'amount' => string '33' (length=2)
          'currency' => string 'USD' (length=3)
          'action' => string 'update' (length=6)
      83 =>
        array
          'name' => string 'Docwell' (length=7)
          'fullName' => string 'Docwell full' (length=12)
          'count' => string '3' (length=1)
          'unit' => string 'THING' (length=5)
          'amount' => string '111111' (length=6)
          'currency' => string 'USD' (length=3)
          'action' => string 'update' (length=6)

How can I use form_validation class to do it?


form_validation with assoc arrays - El Forum - 06-01-2009

[eluser]MDomansky[/eluser]
Something like this

Code:
$this->form_validation->set_rules('specification[][name]', 'lang:project_load_recipient', 'required|min_length[1000]');

or this

Code:
$this->form_validation->set_rules('specification[(:num)][name]', 'lang:project_load_recipient', 'required|min_length[1000]');



form_validation with assoc arrays - El Forum - 06-23-2009

[eluser]MDomansky[/eluser]
Can anybody help me?


form_validation with assoc arrays - El Forum - 06-23-2009

[eluser]TheFuzzy0ne[/eluser]
Is this not what you're after?
Code:
$this->form_validation->set_rules($specification[$num]['name'], 'lang:project_load_recipient', 'required|min_length[1000]');



form_validation with assoc arrays - El Forum - 06-23-2009

[eluser]MDomansky[/eluser]
In my controller I don't know the count of specifications and their ID's.


form_validation with assoc arrays - El Forum - 06-23-2009

[eluser]TheFuzzy0ne[/eluser]
Then how can you validate something when you don't know what to validate it against? That doesn't make sense to me.


form_validation with assoc arrays - El Forum - 06-24-2009

[eluser]MDomansky[/eluser]
I know that my input array has structure specification[(:num)][name], where name - field, (:num) - numeric key.

I want to work with validation like I work with route's rules
Code:
$route['user/(:num)/edit'] = "user/edit/index/$1";
$route['user/(:num)/save'] = "user/edit/save/$1";



form_validation with assoc arrays - El Forum - 06-24-2009

[eluser]TheFuzzy0ne[/eluser]
OK, now I think I understand better. So the array you originally posted isn't something you want to compare to, it's actually the data you want to validate.

I don't think validation is the problem, since it should be simple enough to create a custom validation callback that can loop through the array and validate each part. I think the main issue you're going to experience is giving feedback to the user. I don't think you'll be able to do this easily without overriding the validation class, or creating your own.