嘿,
symfony在两个步骤中填写表单的正确性是什么?假设我们有一个名为Enterprise的实体,我们想要创建一个只包含必填字段的表单和另一个表单,当用户登录时可以填写其他非必填字段。
正确表是怎样的?现在我有一个表单来注册('lib/form/doctrine/EnterpriseForm.class.php')和另一个('lib/form/doctrine/EnterpriseCompleteForm.class.php').In,每个类我们设置标签,验证器,...但问题是第二种形式。当我尝试提交时,它会给我一个错误,因为我没有在模型中定义post必填字段。我该怎么做呢?这条路对吗?我该如何解决这个问题呢?
谢谢。
发布于 2010-01-07 22:31:30
嗯,Felix,我做了“未设置”的更改,它工作得很好……但我有个问题。我尝试对相同的操作进行更新。我的代码看起来是这样的。
actions中的
public function executeStepOne(sfWebRequest $request){
$this->form = new CustomerFormStepOne();
if ($request->isMethod(sfRequest::POST)){
$this->processRegisterForm($request, $this->form,'paso2');
}else{
$this->customer = Doctrine::getTable('Customer')-> find(1);
$this->forward404Unless($this->customer);
}
}processRegisterForm代码所在的 :
protected function processRegisterForm(sfWebRequest $request, sfForm $form,$route)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
$customer = $form->save();
$this->redirect('customer/'.$route);
}
}如果我尝试这样做,他们会返回一个错误'primary key duplicate‘。
发布于 2010-01-07 00:49:16
您应该在第二个表单中取消设置每个不需要的表单字段(当然,您应该保留一个包含记录ID的隐藏字段)。
基本上,您只需使用第二个表单更新记录,因此数据库中的每个必填字段都已作为一个值。
如果你张贴第二个表单的代码会有所帮助。
所以总而言之,你的方法是有效的(也许还有更好的方法,我不知道),只要确保你的代码是正确的。
编辑:
因此,如果我没弄错的话,那么您在代码中使用的表单将更新现有的对象。您应该将此对象传递给表单,知道该对象已经存在,并且可以相应地验证值:
public function executeStepOne(sfWebRequest $request){
$this->customer = Doctrine::getTable('Customer')->find(1);
$this->forward404Unless($this->customer);
$this->form = new CustomerFormStepOne($this->customer);
if ($request->isMethod(sfRequest::POST)){
$this->processRegisterForm($request, $this->form,'paso2');
}对于重复键错误,如果该表的主键自动递增,请检查您的数据库定义。
https://stackoverflow.com/questions/2014468
复制相似问题