我有表格行,我希望用户能够通过用表单的记录值填充表单(表名称:供应商(来编辑,然后提交编辑后的数据。
我已经阅读并重读了Symfony 1.4文档,但我遇到了麻烦:1.( 使用现有记录数据填充表单字段。2.( 保存编辑后的表单。
下面的表单是用于创建供应商的表单,据我了解,编辑现有记录是可以接受的。
至于问题#1,我尝试将现有记录值(在__construct()
操作中(携带到表单中,然后使用$this->setDefaults
将它们设置为默认值,该在重定向到编辑操作中的表单时填充表单输入,但是当用户尝试重定向到创建操作中的表单时,表单不起作用, 这表示找不到默认值。
还值得注意的是,该模块包含多种形式。我使用表单创建记录没有问题,只有在创建后才能编辑它们的值。
有什么方向或建议吗?
下面的代码。
控制器:
public function executeVendorEdit(sfWebRequest $request)
{
$ind_vendor = VendorPeer::retrieveByPK(($request->getParameter('id')));
$this->form = new VendorCreateForm(array('name' => $ind_vendor));
if ($request->isMethod('post')) {
$this->form->bind($request->getParameter('vendor'));
if ($this->form->isValid()) {
$vendor = $this->form->save();
$this->redirect('catalog/vendorEdit?id=' . $vendor->getId());
}
}
$this->setTemplate('vendorEdit');
}
形式:
class VendorCreateForm extends SmartForm
{
protected $is_authenticated = null;
public function __construct($is_authenticated = false)
{
$this->is_authenticated = $is_authenticated;
parent::__construct();
}
public function setup()
{
$this->setWidgets(array(
'name' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
'contact_name' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
'contact_email' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
'contact_phone' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
'address1' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
));
$this->setValidators(array(
'name' => new sfValidatorString(array('required' => true)),
'contact_name' => new sfValidatorString(array('required' => true)),
'contact_email' => new sfValidatorEmail(array('required' => true)),
'contact_phone' => new sfValidatorString(array('required' => true)),
'address1' => new sfValidatorString(array('required' => true)),
));
$this->widgetSchema->setNameFormat('vendor[%s]');
$this->setDefaults(array(
));
parent::setup();
}
}
错误:
致命错误:在第 72 行的/projects/fun-project/src/apps/operations/modules/catalog/actions/actions.class.php 中调用未定义的方法 VendorCreateForm::save((
经过长时间和挫折,我解决了这两个问题。使用值填充表单发生在表单中。它需要在构造操作中将供应商值传递到窗体,然后在设置操作中设置默认值(如果存在供应商值(:if ($this->vendor)
。
注意:$is_authenticated
声明和构造不是必需的(检查我在问题中包含的表格(。
形式:
class VendorCreateForm extends SmartForm
{
protected $vendor = null;
public function __construct($vendor = false)
{
$this->vendor = $vendor;
parent::__construct();
}
public function setup()
{
$this->setWidgets(array(
'name' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
'contact_name' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
'contact_email' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
'contact_phone' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
'address1' => new sfWidgetFormInput(array(),
array('class' => 'form-control')),
));
$this->setValidators(array(
'name' => new sfValidatorString(array('required' => true)),
'contact_name' => new sfValidatorString(array('required' => true)),
'contact_email' => new sfValidatorEmail(array('required' => true)),
'contact_phone' => new sfValidatorString(array('required' => true)),
'address1' => new sfValidatorString(array('required' => true)),
));
$this->widgetSchema->setNameFormat('vendor[%s]');
if ($this->vendor)
{
$this->setDefaults(array(
'name' => $this->vendor->getName(),
'contact_name' => $this->vendor->getContactName(),
'contact_email' => $this->vendor->getContactEmail(),
'contact_phone' => $this->vendor->getContactPhone(),
'address1' => $this->vendor->getAddress1(),
));
}
parent::setup();
}
在控制器中正确保存编辑的表单。正确的 executeVendorEdit 操作与我的 executeVendorCreate 操作几乎相同,但有两个区别:1.( 通过 Propel Query $this->vendor = VendorPeer::retrieveByPK($request->GetParameter('id'))
检索当前对象和 2.(不是声明新的模型对象,而是将值保存到当前对象$this->vendor
。代码如下。
控制器
public function executeVendorEdit(sfWebRequest $request)
{
$this->vendor = VendorPeer::retrieveByPK($request->GetParameter('id'));
$this->form = new VendorCreateForm($this->vendor);
if ($request->isMethod('post')) {
$this->form->bind($request->getParameter('vendor'));
if ($this->form->isValid())
{
$values = $this->form->getValues();
$this->vendor->setName($values['name']);
$this->vendor->setContactName($values['contact_name']);
$this->vendor->setContactEmail($values['contact_email']);
$this->vendor->setContactPhone($values['contact_phone']);
$this->vendor->setAddress1($values['address1']);
$this->vendor->setCreatedTs('now');
$this->vendor->save();
$this->redirect('catalog/vendors');
}
}
}