CodeIgniter 3 - Config文件无法运行的可调用表单验证

时间:2015-06-01 23:56:23

标签: php forms codeigniter validation codeigniter-3

callable form validation feature时,我无法让validation rules are placed in a separate config file CodeIgniter 3工作。

我收到以下错误消息:

遇到PHP错误 严重性:通知
消息:未定义属性:CI_Config :: $ form_validation_callback_library

带有表单验证规则的配置文件如下(config / fvalidation.php):

$config['client_details'] = array(
    array(
            'field' => 'client_abn',
            'label' => 'Client ABN',
            'rules' => array('trim', 'required', array('abn_callable', array($this->form_validation_callback_library, 'abn_check'))),
            'errors' => array('abn_callable' => 'Invalid ABN has been entered %s.')
    )

);

尝试调用的表单验证类是(即$ this-> form_validation_callback_library):

class Form_validation_callback_library
{

    public function abn_check()
    {

        $this->load->library('abn_validator');

        $abn = $this->input->post_get('abn', TRUE);

        if (!$this->abn_validator->isValidAbn($abn)) {
            return FALSE;
        }

        return TRUE;

    }


}

控制器是:

        $this->config->load('fvalidation');
        $validation_rules = $this->config->item('client_details');
        $this->form_validation->set_rules($validation_rules);               

        if ($this->form_validation->run() == FALSE) {
            // show form
        } else {
            // process form data
        }

非常感谢任何帮助。

干杯, VeeDee

5 个答案:

答案 0 :(得分:1)

我会在回调

下面使用codeigniter回调示例

http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

<?php

class Example extends CI_Controller {

public function index() {
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('client_abn', 'ABN Number', 'required|callback_checkabn');

    if ($this->form_validation->run() == FALSE) {

        $this->load->view('something');

    } else {

        // Redirect to success page i.e login or dashboard or what ever

        redirect('/'); // Currently would redirect to home '/'

    }
}

public function checkabn() {

    $this->load->library('abn_validator');

    $abn = $this->input->post('abn');

    if (!$this->abn_validator->isValidAbn($abn)) {
        $this->form_validation->set_message('checkabn', 'Invalid ABN has been entered %s.');
        return FALSE;
    } else {
        return TRUE;
    }

}

}

在您或以上的观点中添加

<?php echo validation_errors('<div class="error">', '</div>'); ?>

<form action="<?php echo base_url('example');?>" method="post">
    <input type="text" name="client_abn" placeholder="" value="" />
</form>

答案 1 :(得分:1)

这是我们在CI中运行自定义表单验证时遇到的最常见问题。无论回调函数是在同一个控制器中还是在回调函数库中,我们都需要传递包含回调函数的类的可访问对象。 所以当你运行

$callable_validations = new Form_validation_callback_library();

$this->form_validation->run($callable_validations)

答案 2 :(得分:0)

目前在CodeIgniter 3上看起来不可能。

我已经创建了一个粗略的解决方法..所以请继续改进它,因为它看起来不漂亮:)。

像这样更新配置文件(/config/fvalidation.php):

$config['client_details'] =  = array(
        array(
                'field' => 'client_abn',
                'label' => 'Client ABN',
                'rules' => array('trim', 'required', array('abn_callable', array("library:form_validation_callback_library", 'abn_check'))),
                'errors' => array('abn_callable' => 'Invalid %s has been entered .')
        )
);

请注意上面配置文件中的以下行,因为我们将在控制器代码中将它们用作标志:

array('abn_callable', array("library:form_validation_callback_library", 'abn_check'))

除了加载实例(/libraries/Form_validation_callback_library.php)之外,库几乎完全相同:

class Form_validation_callback_library
{
    private $_CI;

    function Form_validation_callback_library() {
        $this->_CI =& get_instance();

        log_message('info', "Form_validation_callback_library Library Initialized");
    }

    public function abn_check($abn)
    {

        $this->_CI->load->library('abn_validator');


        if (!$this->_CI->abn_validator->isValidAbn($abn)) {
            return FALSE;
        }

        return TRUE;    
    }

}

在控制器中我们加载库(/controllers/Foo.php):

// load the config file and store
$this->config->load('fvalidation', TRUE);
$rule_dataset = $this->config->item('client_details', 'fvalidation');

// search and load the 'callable' library
foreach ($rule_dataset as $i => $rules) {
    if (isset($rules['rules'])) {
        foreach ($rules['rules'] as $k => $rule) {
            if (is_array($rule) && preg_match("/_callable/",$rule[0]) && isset($rule[1][0])) {                      
                list ($load_type, $load_name) = explode(":", $rule[1][0]);                      
                // load the library
                $this->load->$load_type($load_name);                        
                $rule_dataset[$i]['rules'][$k][1][0] = $this->$load_name;

            }
        }
    }           
}

// set the rules
$this->form_validation->set_rules($rule_dataset);

// load the form
if ($this->form_validation->run() == FALSE) {
  // show form
} else {
    // process form data
}

答案 3 :(得分:0)

我做了类似于Vidura的事情,但通过使用以下代码添加MY_Form_validation.php扩展了表单验证库

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class GS_Form_validation extends CI_Form_validation {

    public function set_rules($field, $label = '', $rules = array(), $errors = array())
    {
        if (is_array($rules))
        {
            foreach ($rules as &$rule)
            {
                if (is_array($rule))
                {
                    if (is_array($rule[1]) and is_string($rule[1][0]))
                    {
                        // handles rule like ['password_check', ['library:passwords', 'check_valid_password']]
                        // You would set_message like $this->form_validation->set_message('password_check', 'Incorrect password');
                        // The advantage of defining the rule like this is you can override the callback functions error message
                        list ($load_type, $load_name) = explode(":", $rule[1][0]);
                        $CI =& get_instance();
                        $CI->load->$load_type($load_name);
                        $rule[1][0] = $CI->$load_name;
                    }
                    else if (is_string($rule[0]))
                    {
                        // handles rule like ['library:password', 'check_valid_password']
                        // You would set_message like $this->form_validation->set_message('check_valid_password', 'Incorrect password');
                        list ($load_type, $load_name) = explode(":", $rule[0]);
                        $CI =& get_instance();
                        $CI->load->$load_type($load_name);
                        $rule[0] = $rule[1];
                        $rule[1] = [$CI->$load_name, $rule[1]];
                    }
                }
            }
        }

        return parent::set_rules($field, $label, $rules, $errors);
    }
}

然后您可以定义对库函数的回调,如:

$this->form_validation->set_rules(['library:passwords', 'check_valid_password']);

其中密码是库,check_valid_password是方法。

答案 4 :(得分:0)

我只是做(config / form_validation.php):

{{1}}

它对我很有用......

我在Codeigniter 3.0.4上运行