简体   繁体   English

codeigniter(和回调)中的form_validation配置文件

[英]form_validation config file in codeigniter (and callbacks)

After abusing Google for over an hour, I've found no answers to this question : 在滥用谷歌超过一个小时后,我找不到这个问题的答案:

When using form_validation.php to your validation rules, Is it possible to pass a dynamic value to the callbacks? 将form_validation.php用于验证规则时,是否可以将动态值传递给回调?

        array(
                    'field' => 'passwordrepeat',
                    'label' => 'סיסמא חוזרת',
                    'rules' => 'passwordsMatch['myDynamicValue']'
            ),

This clearly doesn't work as it passes "myDynamicValue" as a string. 这显然不起作用,因为它将“myDynamicValue”作为字符串传递。 Now, because this config file is loaded so early, this only available resource in it is CI_Loader, which doesn't help much, So I can't access the input class. 现在,因为这个配置文件很早就加载了,所以这个唯一可用的资源就是CI_Loader,这对它没什么帮助,所以我无法访问输入类。

So my question: Can a dynamic value pass to the config file, Or should that rule be written inline in the controller itself? 所以我的问题:动态值是否可以传递给配置文件,或者该规则是否应该在控制器本身内联写入?

    $this->form_validation->set_rules('password1', 'Password', 'trim|required|matches[password2]');
    $this->form_validation->set_rules('password2', 'Verify Password', 'trim|required');

This is what I have for setting form validation on two passwords. 这就是我在两个密码上设置表单验证的方法。 This is what is what comes after you set all of your rules 这就是您设置所有规则后的内容

if ($this->form_validation->run() == FALSE)
    {
        //Validation failed
    }
else
   {
       //Validation suceeded carry on
   }

Here is a link to some documentation 这是一些文档的链接

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

The answer to this question is a bit embarrassing. 这个问题的答案有点令人尴尬。

The solution to this is that you can pass other field names to your callbacks. 解决方案是您可以将其他字段名称传递给回调。 However, remember that what you're passing is the field and not the actual value. 但是,请记住,您传递的是字段而不是实际值。

To get the actual value you'll need to access it through $_POST['field']. 要获得实际值,您需要通过$ _POST ['field']访问它。

For example the built in Matches function 例如内置匹配功能

    public function matches($str, $field)
    {
        if ( ! isset($_POST[$field]))
        {
            return FALSE;
        }

        $field = $_POST[$field];

        return ($str !== $field) ? FALSE : TRUE;
    }

I feel a bit silly :) 我觉得有点傻:)

The answer to the question is little tricky but it's easy to understand. 这个问题的答案有点棘手,但很容易理解。 The solution for the asked question is here. 问题的解决方案就在这里。

Create a form_validation.php file under the application/config/ folder. application/config/文件夹下创建一个form_validation.php文件。 and past the code as bellow. 过去的代码如下。

$config = array(
               array(
                'field' => 'passwordrepeat',
                'label' => 'סיסמא חוזרת',
                'rules' => 'passwordsMatch['myDynamicValue']'
               ),
            );

the rules will loaded automatically available to the $this->form_validation->run(); 规则将自动加载到$this->form_validation->run(); this method. 这种方法。

Also you can append more array, I mean more rules for the different controller. 你也可以附加更多数组,我的意思是更多不同控制器的规则。

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM