2013-04-22 35 views
0

我寫幾個文件來形成提交。但表單驗證程序不適用於選擇(付款狀態)。symfony 1.4 sfValidatorChoice不工作

這是我的表格。

class bookingAddForm extends sfForm 
{ 
    protected static $paymentStatus = array(
     0    => 'Select Payment Status', 
     'PAID'   => 'Paid', 
     'PARTIALLY_PAID' => 'Partially Paid', 
     'NOT_PAID'  => 'Not Paid', 
    ); 

    public function configure() 
    { 
     $this->widgetSchema['check_in'] = new sfWidgetFormInputText(); 
     $this->widgetSchema['check_out'] = new sfWidgetFormInputText(); 

     $this->widgetSchema['payment_status'] = new sfWidgetFormSelect(array('choices' => self::$paymentStatus)); 

     unset($paymentStatus[0]); 

     $this->validatorSchema['check_in'] = new sfValidatorDate(array(
                'min'   => strtotime('today'), 
                'date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~' 
               ), 
               array(
                'required' => 'From date is required', 
                'bad_format' => '"%value%" does not match the date format (DD/MM/YYYY).', 
                'min'  => 'Invalid Date', 
               ) 
              ); 
     $this->validatorSchema['check_out'] = new sfValidatorDate(array(
                'min'   => strtotime('today'), 
                'date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~' 
               ), 
               array(
                'required' => 'To date is required', 
                'bad_format' => '"%value%" does not match the date format (DD/MM/YYYY).', 
                'min'  => 'Invalid Date' 
               ) 
              ); 

     $this->validatorSchema['payment_status'] = new sfValidatorChoice(array('choices' => array_keys(self::$paymentStatus))); 

     $this->widgetSchema->setNameFormat('reservation[%s]'); 
    } 

} 

這是我的行爲。

public function executeAddReservation(sfWebRequest $request) 
    { 
     $chaletId = $request->getParameter('id'); 
     $this->chalet = skiChaletTable::getInstance()->getChalet($chaletId); 

     $this->form = new bookingAddForm(); 

     if ($request->getMethod() == sfRequest::POST) {   
      $this->form->bind($request->getParameter($this->form->getName())); 
      if ($this->form->isValid()) { 
       die('submitted'); 
      } 
     } 
    } 

這是我的看法

<div class="form"> 
    <form id="owner-add-reservation" action="<?php print url_for('owner_add_reservation', array('id' => $chalet->getId())); ?>" method="post"> 
     <?php echo $form->renderHiddenFields(); ?> 

     <div class="row"> 
      <div class="col"> 
       <?php echo $form['check_in']->renderLabel(); ?><br/> 
       <?php echo $form['check_in']->render(); ?> 
       <a href="javascript:void(0);" id="check_in_picker"><img style="vertical-align:middle;" src="/images/icon_checkin_checkout.jpg"></a> 
       <span id="<?php echo 'form_error_' . $form->getName() . '_check_in' ?>" class="form_error"> 
        <?php echo $form['check_in']->getError(); ?> 
       </span> 
      </div> 
      <div class="col"> 
       <?php echo $form['check_out']->renderLabel(); ?><br/> 
       <?php echo $form['check_out']->render(); ?> 
       <a href="javascript:void(0);" id="check_out_picker"><img style="vertical-align:middle;" src="/images/icon_checkin_checkout.jpg"></a> 
       <span id="<?php echo 'form_error_' . $form->getName() . '_check_out' ?>" class="form_error"> 
        <?php echo $form['check_out']->getError(); ?> 
       </span> 
      </div>    
     </div> 

     <div class="row"> 
      <div class="col"> 
       <?php echo $form['payment_status']->renderLabel(); ?><br/> 
       <?php echo $form['payment_status']->render(); ?>     
       <span id="<?php echo 'form_error_' . $form->getName() . '_payment_status' ?>" class="form_error"> 
        <?php echo $form['payment_status']->getError(); ?> 
       </span> 
      </div> 

     </div> 

     <div class="row"> 
      <div class="col float-right"> 
       <input type="submit" value="Add Reservation" class="button_black"/> 
      <div class="col"> 
     </div> 
    </form> 
    <div class="clearfix"></div> 
</div> 

驗證是CHECK_IN和CHECK_OUT作品。但它不適用於payement_status。請幫幫我。

+0

你說的意思是什麼「不加工」。你得到什麼確切的問題? – 2013-04-22 12:32:28

+0

付款狀態驗證無效。其他人顯示錯誤消息。 – 2013-04-22 12:44:44

+0

「不工作」不是我的問題的答案;)你對現場有什麼價值?你期望什麼結果?實際結果是什麼? – 2013-04-22 14:06:32

回答

2

這在窗體的配置代碼行()方法

unset($paymentStatus[0]); 

沒有任何影響,因爲你引用了驗證器的選項的形式靜態變量自:: $ paymentStatus。你應該改變你的sfValidatorChoice以便其選項

array('choices' => array_keys($paymentStatus)) 

,而不是

array('choices' => array_keys(self::$paymentStatus)) 

或者什麼可能是更容易的做到這一點:

protected static $paymentStatus = array(
    'PAID'   => 'Paid', 
    'PARTIALLY_PAID' => 'Partially Paid', 
    'NOT_PAID'  => 'Not Paid', 
); 

public function configure() 
{ 
    //.... 
    $this->widgetSchema['payment_status'] = new sfWidgetFormSelect(array('choices' => array_merge(array('' => 'Select a payment status'), self::$paymentStatus))); 
    //... 
} 
+0

謝謝。其實這是我的語法問題,你已經顯示。 – 2013-04-26 06:33:56