views:

315

answers:

2

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  A: 

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.

Felix Kling
A: 

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'.

nebur85
You should edit your question the next time, not answer yourself. Anyway: This is the first formstep where you only show the required fields, right? How does your template looks like? `Doctrine::getTable('Customer')-> find(1)` looks wrong to me as you always load the customer with ID 1.
Felix Kling
Sorry Felix, next time i will edit my question ;)Yes, i know that only load customer where code is equal to 1 (this code is result of test and test and test, surely is not correct). No, this form only show two fields (isn't the first form) and in my template i have only two fields and the showHiddenFields() statement. I see code generated and there are customercode field with value but the problem is when they validate: they do correct but (pass validation) but print me an error 'primary_key duplicate' or similar... thanks a lot Felix.
nebur85
See my edited answer...
Felix Kling
thanks Felix, i try to do this.
nebur85
Thanks a lot Felix!!! It works fine!!! :)
nebur85
Well accepting your own answer although you provide no solution is not very nice though...
Felix Kling