1

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

  1. custom validation function
  2. 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?

3
  • return !($str !== $field) Commented Aug 24, 2014 at 6:18
  • Actually it will return false in the if ! isset($_POST[$field]) Commented Aug 24, 2014 at 6:26
  • I was thinking of another way to write that, not relevant to your problem. Commented Aug 24, 2014 at 6:26

2 Answers 2

2

Yes, i have a similar problem and there is no way CI core input can solve that, I solved mine not by creating a custom callback function it clutters the controller often, but by extending the Form_validation class MY_Form_validation

then i created a function which i called matches_array then used as matches_array[inputkeyname---inputkeyvalue]

os you would write yours as

$this>form_validation>set_rules("input[password]","Password",'required|matches_array[input---conf_password]');

Here is the function as i remember it.

public function matches_array($str, $field)
{
    $field = explode('---',$field);
    if ( ! isset($theField = $_POST [$field[0] ][ $field[1] ]))
    {
        return FALSE;
    }  
    return ($str !== $theField) ? FALSE : TRUE;
}

EDIT

Put it on your app/libraries and name it MY_Form_validation, MY_ is what you defined in your config. anything you put in here will be automatically added to the rules.

class MY_Form_validation extends CI_Form_validation 
{

    public function __construct($rules = array())
    {
        parent::__construct($rules);
        $this->CI->lang->load('MY_form_validation');
    }

    //your custom functions
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 seems like a good idea. Where did you put your MY_CI_Input?
look at my edit, i have a similar answer here stackoverflow.com/questions/21046166/… sorry it was CI_Form_validation not CI_Input i changed my answer
0

You can Edit MY_Form_validation

public function matches($str, $field)

    {
        return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
        ? ($str === $this->_field_data[$field]['postdata'])
        : FALSE;
    }

1 Comment

Did you actually run this code or your just pulled it out of your head?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.