joomla - 如何返回编辑表单?

标签 joomla

我的 Controller (管理员)中有此代码:

function save(){
        $model = $this->getModel('mymodel');

        if ($model->store($post)) {
            $msg = JText::_( 'Yes!' );
        } else {
            $msg = JText::_( 'Error :(' );
        }
        $link = 'index.php?option=com_mycomponent&view=myview';
        $this->setRedirect($link, $msg);
}

在模型中我有:

function store(){
        $row =& $this->getTable();

        $data = JRequest::get('post');
        if(strlen($data['fl'])!=0){
            return false;
        }

        [...]

这是有效的 - 生成错误消息,但它返回到项目 ListView 。我想保留输入数据的编辑 View 。怎么做?

最佳答案

在您的 Controller 中,您可以:

if ($model->store($post)) {
   $msg = JText::_( 'Yes!' );
} else {
    // stores the data in your session
    $app->setUserState('com_mycomponent.edit.mymodel.data', $validData);

    // Redirect to the edit view
    $msg = JText::_( 'Error :(' );
    $this->setError('Save failed', $model->getError()));
    $this->setMessage($this->getError(), 'error');
    $this->setRedirect(JRoute::_('index.php?option=com_mycomponent&view=myview&id=XX'), false));
}

然后,您需要使用以下内容从 session 加载数据:

JFactory::getApplication()->getUserState('com_mycomponent.edit.mymodel.data', array());

通常这是在模型中的“loadFormData”方法中加载的。在哪里加载该数据将取决于您如何实现组件。如果您使用 Joomla 的表单 api,那么您可以将以下方法添加到您的模型中。

protected function loadFormData()
{
    // Check the session for previously entered form data.
    $data = JFactory::getApplication()->getUserState('com_mycomponent.edit.mymodel.data', array());

    if (empty($data)) {
        $data = $this->getItem();
    }

    return $data;
}

编辑:

但请注意,如果您的 Controller 继承自“JControllerForm”,Joomla 的 API 已经可以为您完成所有这些操作,您无需重写 save 方法。创建组件的最佳方法是复制 Joomla 核心组件中的内容,例如 com_content

关于joomla - 如何返回编辑表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810032/

相关文章:

joomla - 从另一个组件获取参数

javascript - Jquery购物车按钮添加删除

joomla - 构建原生 Joomla 3.x 组件

php - Joomla JDatabase 查询插入空行

zend-framework - 围绕/并行 Zend Framework 应用程序构建 Joomla 网站

php - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : NO) in Joomla when using another user account

php - 如何使用正则表达式从数组中找到匹配的键

jquery - MySql 的 UNION 和 ORDER

php - 服务器配置中/usr/sbin/sendmail -t -i 和/usr/sbin/sendmail 有什么区别?

mysql - 如何在 Joomla/mysql 中删除垃圾邮件用户