What is correctness in symfony to fill a form in two steps?

青春壹個敷衍的年華 提交于 2020-01-17 03:26:08

问题


Hy,

What is correctness in symfony to fill a form in two steps? Imagine that we have a entity called Enterprise and we want to create a form with only required fields and another form, that when the user login can fill the other non-required fields.

How is the correctness form? Now i have a form to registration ('lib/form/doctrine/EnterpriseForm.class.php') and another ('lib/form/doctrine/EnterpriseCompleteForm.class.php').In every class we set labels, validators, ... but the problem is in the second form. When i try to submit it gives me an error because i have'nt post required fields defined in model. How can i do that? Is that way correct? How can i fix this?

thanks.


回答1:


You should unset every non needed form field in the second form (of course you should keep a hidden field with the ID of the record).
Basically you just update the record with the second form so every required field in your database already as a value.

It would help if you post the code of the second form.

So in summary your approach is valid (maybe there are better ways I don't know), just make sure that your code is correct.


Edit:

So if I got you correctly then the form you use in your code updates an existing object. You should pass this object to the form knows, that the object already exists and can validate the values accordingly:

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'); 
}

For the duplicate key error, check your database definition if the primary key of this table gets incremented automatically.




回答2:


Well Felix, i do it "unset" changes and it works fine... but i have a problem. I try to do update on the same action. My code looks like that.

in 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);
    }   
  }

where processRegisterForm code is :

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);
    }
  }

if i try to do this they returns me an error 'primary key duplicate'.



来源:https://stackoverflow.com/questions/2014468/what-is-correctness-in-symfony-to-fill-a-form-in-two-steps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!