0
我有一個笨和DataMapper的一個奇怪的問題,笨&DataMapper的:表單保存的行爲奇怪
我想提出一個CMS系統,我想編輯使用DataMapper的數據庫的文章。當我點擊編輯文章時,如果沒有點擊SAVE按鈕,就會收到'您已成功保存文章'的消息,並且所有數據都將變爲空白並留下0.當我輸入數據並單擊保存時,它會再次保存當我嘗試編輯一遍,又回到0 ...有人可以幫忙請
這裏是控制器
public function edit($id = NULL)
{
// Get articles by ID
$articles = new Article_model();
$article = $articles->where('id', $id)->get();
if ($id)
{
$id == NULL || $article;
count($article) || $error = 'Page not found ';
}
else
{
$article = $this->article_model->get_new();
}
$article->title = $this->input->post('title');
$article->text = $this->input->post('text');
if ($article->save())
{
echo 'You have succesfully saved the article';
}
else
{
echo 'Sorry something went terribly worng';
}
$data = array(
'admin_content' => 'admin/article/edit',
'article' => $article,
);
$this->parser->parse('admin/template_admin', $data);
}
,在這裏我的編輯功能是我的看法
<?php if ($this->tank_auth->is_logged_in()): ?>
<div class="container">
<div class="row">
<div class="col-md-9">
<h3><?php echo empty($article->id) ? 'Add an article' : 'Edit a Page ' . $article->title ;?></h3>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>Publication Date</td>
<td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td>
</tr>
<tr>
<td>Title</td>
<td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
</tr>
<tr>
<td>Body</td>
<td><?php echo form_textarea('text', set_value('text' , $article->text), 'class="tinymce"'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
</div>
</div>
<?php else: redirect('/auth/login'); ?>
<?php endif; ?>
是的,甚至在嘗試按下保存按鈕之前,它仍然嘗試保存表單驗證,並且它在頂部顯示錯誤。請有人幫忙 – Unsparing
這是因爲每次運行控制器方法edit()時都會調用$ article-> save()方法。代碼中沒有驗證,只能在單擊保存按鈕時才運行,甚至僅在提交表單時運行。顯示的錯誤是什麼? – jcorry
另外,您爲什麼要在視圖中檢查auth->登錄狀態?這在控制器中更有意義......你的邏輯應該在哪裏生存。 – jcorry