簡體   English   中英

如何將變量從動作傳遞到形式

[英]How to pass variable from action to form

我確定我將以錯誤的方式進行操作,但是我需要從sfWidgetFormChoice中的選擇之一中取消設置數組鍵。 將該變量添加到表單的唯一方法是從操作。 這是我所擁有的:

行動:

$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;

形成:

$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
    ));

了解形式和動作:
通常,我們將設置帶有字段的表單,將其打印在html頁面中,然后用數據填充表單。 按下提交表單按鈕會將所有數據發送到您在表單action html屬性中定義的方法。
該方法將接收並獲得$request ,其中包含很多參數以及帶有數據的表單。 這些值將在操作中處理。

讓我們看看它在symfony中是如何工作的:

  • 定義並設置一個symfony表單,就像上面顯示的那樣。 打印表單,並在操作參數中指向提交方法,該方法將接收請求:

    <form action="currentModuleName/update"

  • Symfony將自動將請求發送到模塊的action.class.php ,並將查找並將數據發送到executeUpdate函數。

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

  • 經過一些檢查后,symfony將處理表單並設置結果模板。

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

在模塊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'];
    }
  }


您可以在下面的鏈接中查看與您類似的示例,以了解如何處理sfWidgetFormChoice
現在回答真實的問題,為了選擇已刪除的用戶,請在操作中添加以下代碼:

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



將變量從操作傳遞到表單:
對不起,如果我根本不理解您的問題,但是如果您需要將一些參數從操作傳遞到表單,請將數組中的初始化變量發送給表單構造函數:
將變量傳遞到Symfony表單
對於您的情況,獲取用戶列表,刪除不需要的用戶,然后將未刪除的用戶發送到表單構造函數。
您將需要在configure()函數中再次聲明/覆蓋表單,以便您可以更改表單的初始化。 將相同的代碼復制並粘貼到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