覆盖JControllerForm save()方法以修剪POST数据无效 [英] Overwriting JControllerForm save() Method to Trim POST Data Has No Effect

查看:88
本文介绍了覆盖JControllerForm save()方法以修剪POST数据无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件,该组件具有一个称为MyproductControllerGeneralsetting的控制器,该控制器扩展了JControllerForm.在MyproductControllerGeneralsetting内部,我正在从父类覆盖save方法,以便修改$_POST数据,然后该覆盖方法调用父类的save方法进行实际保存.

I have a component that has a controller called MyproductControllerGeneralsetting which extends JControllerForm. Inside MyproductControllerGeneralsetting I am overwriting the save method from the parent class in order to modify $_POST data and then the overwriting method calls the parent class' save method to do the actual saving.

这是MyproductControllerGeneralsetting中的覆盖方法:

/**
 * We overwrite the saved form data and trim them to avoid spaces
 */
public function save($key = null, $urlVar = null){
    if($_POST['jform']){
        foreach($_POST['jform'] as $key=>&$value){
            $value = trim($value);
        }
    }

    // Finally, save the processed form data (calls JControllerForm-save())
    parent::save('id', $urlVar);
}

问题是,即使我已经用这种覆盖方法修剪了每个POST数据字段,但是如果我提交了一些值,例如'value'(请注意最后的空格),那么它们不会被修剪.

The thing is that even though I've trimmed each POST data field in this overwriting method, if I have some values submitted such as 'value ' (note the space in the end), they are not trimmed.

我已经检查了JControllerForm类的save方法,并且似乎在这里从POST获取数据:

I've checked the save method of the JControllerForm class and it seems to be getting the data from POST here:

$data  = $this->input->post->get('jform', array(), 'array');

也许是原因吗?这是获取缓存的数据还是什么?

Maybe that's the reason? Is this getting cached data or something?

推荐答案

与其尝试直接从$_POST获取值,请尝试以与父类相同的方式获取和设置数据-使用内部指针JInput类的(共享)实例.

Instead of trying to get the values from $_POST directly, try getting and setting the data in the same way the parent class does - using an internal pointer to a (shared) instance of the JInput class.

这是一种经过修改的,有效的,被覆盖的save方法:

Here's a modified, working, overwritten save method:

/**
 * We overwrite the saved form data and trim them to avoid spaces
 */
public function save($key = null, $urlVar = null){
    if($_POST['jform']){

        // Get the original POST data
        $original = JRequest::getVar('jform', array(), 'post', 'array');

        // Trim each of the fields
        foreach($original as $key=>$value){
            $original[$key] = trim($value);
        }

        // Save it back to the $_POST global variable
        JRequest::setVar('jform', $postData, 'post');
    }

    // Finally, save the processed form data
    return parent::save('id', $urlVar);
}

这篇关于覆盖JControllerForm save()方法以修剪POST数据无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆