Welcome Guest, Not a member yet? Register   Sign In
Validation callback that changes value doesn't work on arrays?
#1

[eluser]CodeIgniterNewbie[/eluser]
I have a callback as part of my form validation. Assume this callback changes the value that was passed. If I apply the callback on a non-array field, it works fine. But if I apply the callback on an array field, it doesn't work. Any ideas what might be wrong?
Code:
$this->form_validation->set_rules("foo", 'Foo Field Name', 'callback_change_foo'); // will work
    $this->form_validation->set_rules("foo[]", 'Foo Field Name', 'callback_change_foo'); // will NOT work
    
    public function change_foo($foo)
    {
       return 'some new value for $foo';
    }
#2

[eluser]siptik[/eluser]
if you use a string as the key of the array, it does not work.
#3

[eluser]siptik[/eluser]
And with a numeric key does not work
This works if the array key is numeric and starts with 0 and in numerical order
#4

[eluser]CodeIgniterNewbie[/eluser]
Ah, OK. My array is actually associative. For example, foo[bar][blah][blah]. I still need to validate this. How do I do this?
#5

[eluser]siptik[/eluser]
You must do validation for each field.
For example:
Code:
$this->form_validation->set_rules("v[a]", 'Foo Field Name', 'callback_change_foo');
    $this->form_validation->set_rules("v[b]", 'Foo Field Name', 'callback_change_foo');
    $this->form_validation->set_rules("v[c]", 'Foo Field Name', 'callback_change_foo');

    public function change_foo($foo)
    {
       return "some new value for $foo";
    }

<form method="post" action="">
    <input name="v[b]" type="text">
     <input name="v[a]" type="text">
     <input name="v[c]" type="text">
     <input name="add" type="submit">
</form>

result:
Array ( [v] => Array ( [b] => some new value for sdf [a] => some new value for aaa [c] => some new value for ssss ) [add] => Отправить )
#6

[eluser]siptik[/eluser]
although I think that should do it automatically.
Sorry, my English is bad :red:
#7

[eluser]CodeIgniterNewbie[/eluser]
siptik, have you tried your code? My code is basically the same thing. It's not working for me. If it's working for you, then I am doing something wrong.

[quote author="siptik" date="1347263545"]You must do validation for each field.
For example:
Code:
$this->form_validation->set_rules("v[a]", 'Foo Field Name', 'callback_change_foo');
    $this->form_validation->set_rules("v[b]", 'Foo Field Name', 'callback_change_foo');
    $this->form_validation->set_rules("v[c]", 'Foo Field Name', 'callback_change_foo');

    public function change_foo($foo)
    {
       return "some new value for $foo";
    }

<form method="post" action="">
    <input name="v[b]" type="text">
     <input name="v[a]" type="text">
     <input name="v[c]" type="text">
     <input name="add" type="submit">
</form>

result:
Array ( [v] => Array ( [b] => some new value for sdf [a] => some new value for aaa [c] => some new value for ssss ) [add] => Отправить )[/quote]
#8

[eluser]Beginers[/eluser]
ILL SHARE MY KNOWLEDGE TO YOU ALL. I ALSO USE ARRAY IN MY WEBSITE HERE IT IS CONSIDERING I HAVE A MULTIPLE ARRAYS OF NAME:

VIEW
Code:
<?php echo form_open('controllername/functioname');?>
<input type="text" name="name[0]" value="Name 0"/>
<input type="text" name="name[1]" value="Name 1"/>
<input type="text" name="name[2]" value="Name 2"/>
<input type="text" name="name[3]" value="Name 3"/>
</form>

CONTROLLER
Code:
$countinput = count($this->input->post());
//for loop alternatives
for($i=0;$i<=($countinput-1);$i++):
$this->form_validation->set_rules('name['.$i.']','Names','required|trim|');
endfor;

I hope this help. im just a beginers hehe. if you have any questions fill free to PM me
#9

[eluser]CodeIgniterNewbie[/eluser]
But you have an indexed array, not an associative array.
#10

[eluser]Beginers[/eluser]
(EDITED)

it now on you on how will you manage the code...for an instance

Code:
$characters="abcdefghijklmnopqrstuvwxyz";

$countinput = count($this->input->post());
//for loop alternatives
for($i=0;$i<=(strlen($countinput)-1);$i++):
$this->form_validation->set_rules('name['.$characters[$i].']','Names','required|trim|');
endfor;
//tested
OUTPUT
Code:
name[a]
name[b]
name[c]
name[d]
name[e]
name[f]
name[g]
name[h]
name[i]
name[j]
name[k]
name[l]
name[m]
name[n]
name[o]
name[p]
name[q]
name[r]
name[s]
name[t]
name[u]
name[v]
name[w]
name[x]
name[y]
name[z]




Theme © iAndrew 2016 - Forum software by © MyBB