Question

I'm having a problem when I try to save the form's information into the database. My form doesn't seem to be valid, even after manually setting the Theater's id for every theater in the chosen Network. Here's the related part of my module's actions.class.php :

Here's executeCreate():

public function executeCreate(sfWebRequest $request) {
    $this->form = $this->configuration->getForm();
    $this->showing = $this->form->getObject();
    $this->processCreateForm($request, $this->form);
    $this->setTemplate('new');
} 

and now processCreateForm():

protected function processCreateForm(sfWebRequest $request, sfForm $form) {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

    $form_name = $form->getName();
    $parameters = $request->getParameter($form_name);
    $network_id = $parameters['network_id'];

    $theaters_list = Doctrine_Query::create()
            [...]
            ->execute();

    foreach ($theaters_list as $theater) {
        $form->getObject()->setTheaterId($theater->theater_id);
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

        if ($form->isValid()) {
            $showing = $form->save();
        } else {
            foreach ($form->getErrorSchema()->getErrors() as $key => $error) {
                echo '<p>' . $key . ': ' . $error . '</p>';
            }
        }
    }
    $this->getUser()->setFlash('update_success', true);
    $this->setTemplate('new');
}

Here's the output :

Theater_id required output

Thank you for your help

Was it helpful?

Solution

There are two strange things going on here which I think break your code.

  1. You run the bind() method twice which might be resetting your values on the object.

  2. I don't think that the getObject() method returns the object by reference.

So when you run:

    $form->getObject()->setX($val);
    $form->save();

then you update the field of the object returned by the form, but then save the original object which is still bound to the form.

Try doing something like this:

    $myObject = $form->updateObject()->getObject();
    $myObject->setX($value);
    $myObject->save();

The updateObject() is important if you use the form to edit an existing object, not to create a new one. Without this you will get the old values of the object.

If you want to run it in a loop you can loop only the setting and saving part. So you would have something like this in your processCreateForm:

protected function processCreateForm(sfWebRequest $request, sfForm $form)
{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

    if ($form->isValid()) { //You can check the validity of your form at this point.

        //Create $theatersList
        ...

        $myObject = $form->updateObject();

        foreach ($theatersList as $theater) {
            $myObject->setTheaterId($theater->theater_id);
            $showing = $myObject->save();

            //Do something with $showing

        }
    } else {
         //Print the errors.
    }
}

Using this code you can unset the widget for theatre_id in your form as it should not be set by the user and does not have to be part of the form validation.

EDIT

Some changes to the code:

protected function processCreateForm(sfWebRequest $request, sfForm $form)
{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

    if ($form->isValid()) { //You can check the validity of your form at this point.

        //Create $theatersList
        ...

        $myObject = $form->updateObject();
        $myObjectVars = $myObject->toArray();

        foreach ($theatersList as $theater) {

            $myNewObject = new SomeClass();
            $myNewObject->fromArray($myObjectVars);
            $myNewObject->setTheaterId($theater->theater_id);
            $showing = $myNewObject->save();

            //Do something with $showing

            $myNewObject->free();
            unset($myNewObject);
        }
    } else {
         //Print the errors.
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top