Search code examples
phpzend-frameworkzend-formzend-form-elementzend-form-sub-form

Zend Form, can't take input values if there's a default value in element


I'm using Zend Framework 1.12, making a page which have multiple forms in it. I use a single master form and subforms in it. Therefore, I have just one Validation code part. These subforms point to different tables in database. Aim is that, if there is a row in database about that form, form should take values from database as default, to give user chance of change that data. And if there is not a row in database, input of this form will be inserted to db. At first, I can take values from db and show them as values of form elements. But when I change it and try to take values with

$form->getValues();

I cannot access the values entered (or edited) in page, I just re-access the values in database which was put in form as default. This form should be able to edit always, and I have multiple forms for different kinds of data, which will do same thing too. What must I be doing wrong ? Any idea?

(addition) here is a summary of the relevant piece of my controller code:

$masterform = new Application_Form_GeneralForm(); // a class which extends Zend_Form
$form1 = new Application_Form_SmallForm(); // a class which extends Zend_Form_Subform
$masterform->addSubform($form1, 'form1');
                         // so far, for form 1, no problem. My second form will be 
                         // added to the masterform after this first form is submitted,
                         // which works fine.

$form2 = new Application_Form_AnotherSmallForm(); // a class which extends Zend_Form_Subform

$request = $this->getRequest();
if ($request->isPost()){
    if ($generalform->isValid($request->getPost())) {
         $form2->loadValues(); // the part that form elements are filled with data 
                               // taken from db, a method defined in `AnotherSmallForm` 
                               // class. Just assigning values to elements with `setValue()`

         $form2->saveValues(); // Here is the problem, another method to save the 
// current values to db. (defined in form class). I have to do this in this fragment of code, so i don't know to 
// use which order ( saveValues() and loadValues() methods' order )` 

         $masterform->addSubform($form2, 'form2');
    }
}

So, 1st step: $form1 is added to $masterform.

2nd step: $masterform submitted (it only includes $form1 now), then $form2 is added to $masterform. before it is added, the values for $form2 is loaded inside form elements.

3rd step: $masterform submitted, (so is $form1 and $form2). If any change of the values in $form2 , they must be updated in db by this submission.

This is goal of this code, which could not be accomplished because of 3rd step.


Solution

  • I finally figured it out. Even if subforms are created in conditions respect to each other, they should be created out of the if($request->isPost()) condition block. So, if you have to add them with steps (like I have to do it, $form2 created after $form1 is submitted, and $form1 remains in page), you should test if their requirements are satisfied directly and individually. This approach of mine does not work. It should be like this:

    $masterform = new Application_Form_GeneralForm(); 
    $form1 = new Application_Form_SmallForm();
    $masterform->addSubform($form1, 'form1');                          
    
    $form2 = new Application_Form_AnotherSmallForm(); 
    
    if (/* the requirement you want to check if $form2 should be added or not, 
        and this could easily be checking some value which is submitted with 
        $form1, so you have already checked if $form1 has been submitted */ ) 
        $masterform->addSubform($form2, 'form2'); // we add it if requirement is met
    
    $request = $this->getRequest();
    if ($request->isPost()){
        if ($generalform->isValid($request->getPost())) {
             $form2->saveValues(); // be sure it saves the final values added to form
             ////.......
        }
    }
    

    and, I stopped having loadValues() here, and I call that method inside the init() method of form class. So, as konradwww said, saveValues() should happen first, and loadValues() next. This could done by using the loadValues() in form class, it belongs to the process of initiation of the form anyway.