简体   繁体   English

如何将变量从动作传递到形式

[英]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. 我确定我将以错误的方式进行操作,但是我需要从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
    ));

Understanding forms and actions: 了解形式和动作:
Usually we will setup a form with fields, print it in a html page and fill the form with data. 通常,我们将设置带有字段的表单,将其打印在html页面中,然后用数据填充表单。 Pressing the submit form button will send all the data to a method defined in your form action html attribute. 按下提交表单按钮会将所有数据发送到您在表单action html属性中定义的方法。
The method will receive and get a $request , with a lot of parameters and also the form with the data. 该方法将接收并获得$request ,其中包含很多参数以及带有数据的表单。 Those values will be processed in the action. 这些值将在操作中处理。

Lets look how it exactly works in symfony: 让我们看看它在symfony中是如何工作的:

  • Define and Setup a symfony form, like the one you have shown above. 定义并设置一个symfony表单,就像上面显示的那样。 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 Symfony将自动将请求发送到模块的action.class.php ,并将查找并将数据发送到executeUpdate函数。

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

  • After some checks, symfony will process the form and set a result template. 经过一些检查后,symfony将处理表单并设置结果模板。

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

In the processForm of your module action.class.php , you should process all the received values (request) also with the form: 在模块action.class.phpprocessForm中,您还应该使用以下形式处理所有接收到的值(请求):

  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 . 您可以在下面的链接中查看与您类似的示例,以了解如何处理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 将变量传递到Symfony表单
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. 您将需要在configure()函数中再次声明/覆盖表单,以便您可以更改表单的初始化。 Copy and paste the same code into the configure() function and comment the line: // parent::setup(); 将相同的代码复制并粘贴到configure()函数中,并注释以下行:// 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.
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM