2010-03-17 104 views
0

我在使用Zend_Form構建的表單工作時遇到問題。Zend_Form驗證問題

想法是這樣的:我有兩個下拉菜單。一個是玩家名單。另一個是與玩家扮演相同位置的自由球員列表。我正在使用onChange javascript回調來運行一些Ajax代碼,該代碼將用自己從玩家下拉列表中選擇的玩家的位置上的新代替自由代理列表下拉菜單。

現在,也許這是錯誤的方式,但我通過創建Zend_Form的一個實例,然後創建所有這些將元素添加到窗體的setX方法來構建表單。我的推理是我想在頁面上的特定位置顯示某些元素,而不僅僅是在我的模板上輸出$ this->表單。

問題似乎是當我得到表單發回,驗證器似乎不知道我爲自由代理下拉設置的驗證規則。這裏有一些相關的代碼來看看。我是一個相對的ZF n00b,所以隨時告訴我,如果你跳出來,我不會採取ZF的方式。

在控制器的操作:

public function indexAction() 
{ 
    if ($this->getRequest()->isPost()) { 
     $form = new Baseball_Form_Transactions(); 

     if ($form->isValid($this->_request->getPost())) { 
      $data = $this->_request->getPost(); 
      $leagueInfo = Doctrine::getTable('League')->findOneByShortName($data['shortLeagueName'])->toArray(); 

      // Create the request top drop an existing player  
      $transactionInfo = array(
       'league_id' => $leagueInfo['id'], 
       'team_id' => $data['teamId'], 
       'player_id' => $data['players'], 
       'type' => 'drop', 
       'target_team_id' => 0, 
       'transaction_date' => date('Y-m-d H:m:s') 
      ); 
      $transaction = new Transaction(); 
      $transaction->fromArray($transactionInfo); 
      $transaction->save(); 

      // Now we do the request to add a player 
      $transactionInfo['team_id'] = 0; 
      $transactionInfo['player_id'] = $data['freeAgents']; 
      $transactionInfo['target_team_id'] = $data['teamId']; 
      $transactionInfo['type'] = 'add'; 
      $transaction = new Transaction(); 
      $transaction->fromArray($transactionInfo); 
      $transaction->save(); 
      $this->_flashMessenger->addMessage('Added transaction'); 
     } 
    } 

    $options = array(
     'teamId' => $this->teamId, 
     'position' => 'C', 
     'leagueShortName' => $this->league 
    ); 

    $this->transactionForm->setMyPlayers($options); 
    $this->transactionForm->setFreeAgents($options); 
    $this->transactionForm->setTeamId($options); 
    $this->transactionForm->setShortLeagueName($options); 
    $this->view->transactionForm = $this->transactionForm; 
    $this->view->messages = $this->_flashMessenger->getMessages(); 
    $transaction = new Transaction(); 
    $this->view->transactions = $transaction->byTeam($options); 
} 

接下來我們形式本身

public function setMyPlayers($options) 
{ 
    $data = Doctrine::getTable('Team')->find($options['teamId']); 
    $players = array(); 

    foreach ($data->Players->toArray() as $player) { 
     $players[$player['id']] = "{$player['position']} - {$player['first_name']} {$player['last_name']}"; 
    } 

    $playersSelect = new Zend_Form_Element_Select(
     'players', 
     array(
      'required' => true, 
      'label' => 'Players', 
      'multiOptions' => $players, 
     ) 
    ); 

    $this->addElement($playersSelect); 
} 

public function setFreeAgents($options) 
{ 
    $q = Doctrine_Query::create() 
     ->select('CONCAT(p.first_name, " ", p.last_name) as full_name, p.id, p.position') 
     ->from('Player p') 
     ->leftJoin('p.Teams t') 
     ->leftJoin('t.League l ON l.short_name = ?', $options['leagueShortName']) 
     ->where('t.id IS NULL') 
     ->andWhere('p.position = ?', $options['position']) 
     ->orderBy('p.last_name'); 
    $q->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY); 
    $data = $q->execute(); 
    $freeAgents = array(); 

    foreach ($data as $player) { 
     $freeAgents[$player['id']] = $player['full_name']; 
    } 

    $freeAgentsSelect = new Zend_Form_Element_Select(
     'freeAgents', 
     array(
      'label' => 'Free Agents', 
      'multiOptions' => $freeAgents, 
      'size' => 15 
     ) 
    ); 
    $freeAgentsSelect->setRequired(true); 
    $this->addElement($freeAgentsSelect); 
} 

public function setShortLeagueName($options) 
{ 
    $shortLeagueNameHidden = new Zend_Form_Element_Hidden(
     'shortLeagueName', 
     array('value' => $options['leagueShortName']) 
    ); 
    $this->addElement($shortLeagueNameHidden); 
} 

public function setTeamId($options) 
{ 
    $teamIdHidden = new Zend_Form_Element_Hidden(
     'teamId', 
     array('value' => $options['teamId']) 
    ); 
    $this->addElement($teamIdHidden); 
} 

有一個在形式沒有init或__construct()方法。

我的問題似乎很簡單:如果他們沒有從自由坐席列表中選擇某人,則拒絕表單內容爲無效。現在,它通過有效。我花了相當長的時間在網上搜索答案,一直沒有找到答案。

在此先感謝您的幫助。

回答

1

您需要爲每個請求設置表單的字段。試試這個:

public function indexAction() 
{ 
    $form = new Baseball_Form_Transactions(); 
    $options = array(
     'teamId' => $this->teamId, 
     'position' => 'C', 
     'leagueShortName' => $this->league 
    ); 

    $form->setMyPlayers($options); 
    $form->setFreeAgents($options); 
    $form->setTeamId($options); 
    $form->setShortLeagueName($options); 
    $this->transactionForm = $form; 

    if ($this->getRequest()->isPost()) { 

     if ($form->isValid($this->_request->getPost())) { 
      $data = $form->getValues(); 
      $leagueInfo = Doctrine::getTable('League')->findOneByShortName($data['shortLeagueName'])->toArray(); 

      // Create the request top drop an existing player  
      $transactionInfo = array(
       'league_id' => $leagueInfo['id'], 
       'team_id' => $data['teamId'], 
       'player_id' => $data['players'], 
       'type' => 'drop', 
       'target_team_id' => 0, 
       'transaction_date' => date('Y-m-d H:m:s') 
      ); 
      $transaction = new Transaction(); 
      $transaction->fromArray($transactionInfo); 
      $transaction->save(); 

      // Now we do the request to add a player 
      $transactionInfo['team_id'] = 0; 
      $transactionInfo['player_id'] = $data['freeAgents']; 
      $transactionInfo['target_team_id'] = $data['teamId']; 
      $transactionInfo['type'] = 'add'; 
      $transaction = new Transaction(); 
      $transaction->fromArray($transactionInfo); 
      $transaction->save(); 
      $this->_flashMessenger->addMessage('Added transaction'); 
     } 
    } 

    $this->view->transactionForm = $form; 
    $this->view->messages = $this->_flashMessenger->getMessages(); 
    $transaction = new Transaction(); 
    $this->view->transactions = $transaction->byTeam($options); 
} 
+0

感謝羅布......我*知道*我正在做一些微妙的錯誤 – GrumpyCanuck 2010-03-17 22:37:42

1

不具體到你的問題,但只是FYI,$的數據應該從$形式 - >的GetValues()拉,使數據被正確過濾。

+0

是的 - 我在我提供的代碼示例中做到了這一點,但忘記了把它叫出來。 – 2010-03-17 21:54:30