2013-09-29 43 views
0

這是我的形式,我有一個單選按鈕,從1到5(非常糟糕的非常好)如何獲得單選按鈕的值在Zend的形式

parent::__construct($name); 

    $this->setAttribute('method', 'post'); 

    $this->add(array(
     'type' => 'Zend\Form\Element\Radio', 
     'name' => 'rate_box', 
     'options' => array(
      'label' => 'Please choose your rate', 
      'value_options' => array(
       '1' => ' Very Bad', 
       '2' => ' Bad', 
       '3' => ' Fine', 
       '4' => ' Good', 
       '5' => ' Very Good', 
      ), 
     ), 
     'attributes' => array(
      'value' => '1' //set checked to '1' 
     ) 
    )); 

    $this->add(array(
     'name' => 'submit', 
     'type' => 'Submit', 
     'attributes' => array(
      'id' => 'submit', 
      'class' => 'btn btn-primary', 
     ), 
    )); 

這是我的控制器的相關部分

 $form = new VoteForm(); 

     $request = $this->getRequest(); 
     if ($request->isPost()) 
     { 
      $form->setData($request->getPost()); 
      if ($form->isValid()) 
      { 
       $formdata = $form->getData(); 
       $vote = new Vote(); 
       $data = $vote->getArrayCopy(); 
       $data['user_id'] = $user_id; 
       $data['voted_user_id'] = $voted_user_id; 
       $data['ratescore'] = $formdata['rate_box']; //Here I take the value of radion button 

       $vote->populate($data); 
       try 
       { 
        $this->getEntityManager()->persist($vote); 
        $this->getEntityManager()->flush(); 
        return $this->redirect()->toRoute('home',array('user_id' => $user_id, 
                   'action' => 'home', 
        )); 
       } 
       catch(DBALException $e){ 

       } 
      } 
     } 

爲什麼我無法檢索「rate_box」的值,並保存到我的$數據[「ratescore」]? 謝謝!

回答

0

幾小時前我剛剛遇到了這個問題。您可以嘗試在您的操作中使用原生$ _POST ['rate_box']檢索單選按鈕的值。 所以,在你的控制器:

public function yourAction() { 
     $form = new VoteForm(); 
     if($this->getRequest->isPost()) { 
      $data = $this->getRequest->getPost(); 
      if($form->isValid($data)) { 
       $rate = $_POST['rate_box']; 
       // another code.. 
      } 
     } 
    } 

希望這有助於.. :)