gpt4 book ai didi

joomla - 覆盖 JControllerForm save() 方法以修剪 POST 数据没有效果

转载 作者:行者123 更新时间:2023-12-02 07:03:36 24 4
gpt4 key购买 nike

我有一个组件,它有一个名为 MyproductControllerGeneralsetting 的 Controller ,它扩展了 JControllerForm。在 MyproductControllerGeneralsetting 中,我正在覆盖父类中的 save 方法以修改 $_POST 数据,然后覆盖方法调用父类' save 方法进行实际保存。

下面是 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”(注意末尾的空格),它们不会被修剪。

我检查了 JControllerForm 类的 save 方法,它似乎是从这里的 POST 获取数据:

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

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

最佳答案

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

这是一个修改过的、有效的、覆盖的save方法:

/**
* 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);
}

关于joomla - 覆盖 JControllerForm save() 方法以修剪 POST 数据没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16280955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com