Patrick
Patrick

Reputation: 335

How to pass variable from action to form

I am sure I am going about this the wrong way, but I need to unset an array key from one of my choices in a sfWidgetFormChoice. The only way to get that variable to the Form is from the action. Here's what I have:

Action:

$id = $request->getParameter('id');
$deleteForm = new UserDeleteForm();
$choices = array();
$choices = $deleteForm->getWidgetSchema('user')->getAttribute('choices');
unset($choices[$id]);  //I obviously don't want the user to be able to transfer to the user being deleted
$this->deleteForm = $deleteForm;

Form:

$users = Doctrine_Core::getTable('sfGuardUser')->getAllCorpUsers()->execute();
$names = array();
    foreach($users as $userValue){
        $names[$userValue->getId()] = $userValue->getProfile()->getFullName();
    };
//        unset($names[$id]);  //this works, but I can't figure out how to get $id here.
    $this->widgetSchema['user'] = new sfWidgetFormChoice(array(
        'choices'   => $names
    ));
    $this->validatorSchema['user'] = new sfValidatorChoice(array(
        'required'  => true,
        'choices'   => $names
    ));

Upvotes: 0

Views: 1534

Answers (1)

starnetdev
starnetdev

Reputation: 978

Understanding forms and actions:
Usually we will setup a form with fields, print it in a html page and fill the form with data. Pressing the submit form button will send all the data to a method defined in your form action html attribute.
The method will receive and get a $request , with a lot of parameters and also the form with the data. Those values will be processed in the action.

Lets look how it exactly works in symfony:

  • Define and Setup a symfony form, like the one you have shown above. Print the form and in the action parameter point to the submit method which will receive the request:

    <form action="currentModuleName/update"

  • Symfony will automatically send the request to the action.class.php of your module, and will look for and send the data to the function executeUpdate

    public function executeUpdate(sfWebRequest $request){
    //...
    $this->form = new TestForm($doctrine_record_found);
    $this->processForm($request, $this->form);
    }

  • After some checks, symfony will process the form and set a result template.

    processForm(sfWebRequest $request, sfForm $form) { ... }
    $this->setTemplate('edit');

In the processForm of your module action.class.php, you should process all the received values (request) also with the form:

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
         $formValues = $this->form->getValues();
         $Id = $formValues['yourWidgetName'];
    }
  }


You may check the following link for an example like yours, about how to process a sfWidgetFormChoice.
And now answering to the real question, in order to select the deleted users, add the following code in your action:

//process the form, bind and validate it, then get the values.
$formValues = form->getValues();
$choicesId = $formValues['choices'];



Pass variable from action to the form:
Excuse me if I have not understand your question at all but in case you need to pass some parameters from your action to the form, send the initialization variables in an array to the form constructor:
Pass a variable to a Symfony Form
In your case, get the list of users, delete the user you dont want and send the non deleted users to the form constructor.
You will need to redeclare/overwrite your form again in the configure() function so that you could change the initialization of the form. Copy and paste the same code into the configure() function and comment the line: //parent::setup();

class TbTestForm extends BaseTbTestForm
{
  public function configure()
  {
         //.. copy here the code from BaseTbTestForm
         //parent::setup();
         $vusers = $this->getOption('array_nondeleted_users');
         //now set the widget values with the updated user array.
  }
}

Upvotes: 3

Related Questions