簡體   English   中英

symfony 1.4 sfValidatorChoice不起作用

[英]symfony 1.4 sfValidatorChoice is not working

我寫幾個文件來形成提交。 但是表單驗證器不適用於選擇(付款狀態)。

這是我的表格。

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也不起作用。 請幫我。

表單中的這行代碼的configure()方法

unset($paymentStatus[0]);

沒有效果,因為您在驗證器的選項中引用了表單靜態變量self :: $ 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)));
    //...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM