Welcome Guest, Not a member yet? Register   Sign In
Form validation callback parameter
#1

[eluser]jepotski[/eluser]
How can I access other form fields from my callback function?
I have an array of elements and I want to validate the whole row of element based on the value of field1. If field1 has no value then myfunc should return TRUE and will skip any validation from the function.


Code:
$rules = array(
               array(
                     'field'   => 'field1[]',
                     'rules'   => 'required'
                  ), array(
                     'field'   => 'field2[]',
                     'rules'   => 'required|callback_myfunc'
                  ));

function myfunc($field1, ???$field2???){
if($field2=="") return TRUE;
//code extra validations
}
#2

[eluser]sketchynix[/eluser]
You can refer directly to the post variable...

$this->input->post('field1');
#3

[eluser]Drubo[/eluser]
but I have field1[]

now how can I refer this array element?
#4

[eluser]Nick Roper[/eluser]
If you use array notation for form input names then an equivalent array will be created in $_POST. So, the first input named field[] can be referenced as $_POST['fiield'][0] and the next as $_POST['field'][1] etc. You can also specify keys explicitly in the input name - for example:

name="field[2]"

can then be referenced as $_POST[2]

If you want to use associative keys then don't enclose the key name in quotes in the <input> HTML, otherwise the quotes become part of the key name:

name="field[option1]"

can be referenced as $_POST['option1']




Theme © iAndrew 2016 - Forum software by © MyBB