Codeigniter using form validation function “matches” with sub-array POST

喜夏-厌秋 提交于 2021-02-17 03:19:30

问题


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?


回答1:


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
}



回答2:


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;
    }


来源:https://stackoverflow.com/questions/25469030/codeigniter-using-form-validation-function-matches-with-sub-array-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!