您检查过文档吗?特别是this part of the doc?
它通过使用基本的联系表单来涵盖表单系统:
- 你有一个控制器(
actions.class.php 在你的main 模块中),它创建一个表单实例并处理提交(验证、保存等)
- 然后是显示表单的模板 (
contactSuccess.php)
主要区别在于您可能有一个名为 Song 的模型,因此您必须使用 SongForm 而不是创建一个新模型(使用new sfForm())。对于这部分,您可以在同一文档页面上看到 Forms Based on a Model 部分,它涵盖了文章模型的情况。
编辑:
一步一步:
在你的main/actions/actions.class.php:
public function executeIndex($request)
{
$this->form = new SongForm();
if ($request->isMethod('post'))
{
$this->form->bind($request->getParameter('song'));
if ($this->form->isValid())
{
$song = $this->form->save();
$this->getUser()->setFlash('notice', 'Thank you, the song has been added');
$this->redirect('main/index');
}
}
}
在您的模板中,main/templates/indexSuccess.php:
<?php if ($sf_user->hasFlash('notice')): ?>
<div class="flash_notice"><?php echo $sf_user->getFlash('notice') ?></div>
<?php endif ?>
<?php echo $form->renderFormTag('main/index') ?>
<table>
<?php echo $form ?>
<tr>
<td colspan="2">
<input type="submit" />
</td>
</tr>
</table>
</form>
你已经完成了。
我真的鼓励您阅读整个Jobeet tutorial。你会学到很多东西。基本上我在这里描述的所有内容都在本教程中。
对于sf_guard_user字段,您应该将其重新定义为隐藏,然后为当前连接的用户设置一个默认值。
创建一个新表单:/lib/form/CustomSongForm.class.php
<?php
class CustomSongForm extends SongForm
{
public function configure()
{
parent::configure();
$this->widgetSchema['sf_guard_user_ud'] = new sfWidgetFormInputHidden();
}
}
然后你可以定义默认值,就像你说的:
}$this->form->setDefault('sf_guard_user_id', $this->getUser()->getId());