Just started with CI last week and got this issue. What to put inside the matches function if I'm passing the form data as an array?
I use array in the html form to locate all input fields inside single array in case I want to pass user generated input such as multiple phone numbers or emails. So everything is placed in array such as this:
<div>
<label for="password">Password</label>
<input type="password" name="input[password]" id="password" value="<?php echo set_value("input[password]")?>"/>
</div>
<div>
<label for="password">Confirm Password</label>
<input type="password" name="input[conf_password]" id="conf_password" value="<?php echo set_value("input[conf_password]")?>"/>
</div>
Notice the *name="input[password]"*
The validation works like a charm for all except when I use the function matches:
$this->form_validation->set_rules("input[password]", "Password", 'required|matches[input[conf_password]]');
$this->form_validation->set_rules("input[conf_password]", "Confirm Password", 'required');
matches[input[conf_password]]
This will not work because after I checked the Form_Validation.php I found out that matches will take whatever string I put between the square brackets of matches and tries to fetch the value from $_POST directly.
CI codes:
/**
* Match one field to another
*
* @access public
* @param string
* @param field
* @return bool
*/
public function matches($str, $field)
{
if ( ! isset($_POST[$field]))
{
return FALSE;
}
$field = $_POST[$field];
return ($str !== $field) ? FALSE : TRUE;
}
So by right there would be no such thing as $_POST[input[conf_password]].
I'm aware that I can solve this by using
- custom validation function
- compare directly $_POST["input"]["password"] === $_POST["input"]["conf_password"]
I'm not sure what I'm missing since everything in CI related to forms is working nicely with arrays, why wouldn't this function?
!($str !== $field)