我的 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/