【问题标题】:Codeigniter uri language identifier and form_validation not workingCodeigniter uri 语言标识符和 form_validation 不起作用
【发布时间】:2012-11-09 10:19:04
【问题描述】:

我在使用 codeigniter uri 语言标识符和 form_validation 时遇到问题。

问题是当我将 $config['lang_ignore'] 设置为 FALSE 时 form_validation 停止工作。 我已经在一个干净的 codeigniter 安装和设置 uri 语言标识符中进行了测试,如 codeigniter 的 wiki 中所述。

这是我的控制器应用程序/controllers/form_validation_test.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Form_validation_test extends CI_Controller {

public function __construct()
{
    parent::__construct();
}

public function index()
{   
    $this->load->view('form_validation_test_view');
}



//Get Ajax POST:
public function getPost()
{
    $result = FALSE;
    $data = NULL;

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

    $this->form_validation->set_rules('user','User','required');

    $result = $this->form_validation->run();

    $response = array(
        'result' => $result
    );

    $this->output->set_content_type('application/json')
            ->set_output(json_encode($response));
}
}

查看应用程序/views/form_validation_view.php:

<script src="http://ci_test.localhost/js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ci_test.localhost/js/ci_test.js"></script>

<form name="formulario" id="formulario" action="http://ci_test.localhost/form_validation_test/getPost">
    <input type="text" id="user" name="user" value="">
</form>

<input type="button" id="btnSubmit" value="submit">

<div id="result"></div>

和 Javascript 文件 js/ci_test.js:

jQuery(document).ready(function(){

jQuery('#btnSubmit').click(function(){

    var postData    = jQuery('#formulario').serialize();
    var targetUrl   = jQuery('#formulario').attr('action');     

    jQuery.ajax({
        url:        targetUrl,
        data:       postData,
        type:       'post',
        dataType:   'json', 

        success:    function (data, textStatus, jqXHR) {
            res = data.result ? 'TRUE':'FALSE';
            jQuery('#result').html(res);

        }

    });
});
});

如您所见,我通过 ajax 请求发送帖子,只需获取结果并将其放在 div 中。如果将 'lang_ignore' 设置为 TRUE,它会起作用,但如果将其设置为 FALSE,则不会。

我观察到的另一个行为是,当一切正常时,您会在 firebug 中看到 POST 请求,但是当使用 lang_ignore 作为 FALSE 时,您可以看到 POST 请求和仍在加载的 GET 请求。我不知道为什么会发生这种情况,但也许可以帮助某人找到问题。

任何帮助将不胜感激。 感谢大家的宝贵时间。

【问题讨论】:

    标签: php ajax codeigniter internationalization validation


    【解决方案1】:

    我找到了解决方案。

    如果您将 lang_ignore 参数设置为 FALSE,则 application/core/MY_lang.php 文件中的构造函数会执行一次重定向(如果我错了,请纠正我)您会丢失 POST。

    因此,在一个请求中直接包含语言段。

    http://ci_test.localhost/en/form_validation_test/getPost
    

    它对我有用,希望可以帮助某人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      相关资源
      最近更新 更多