0

我写了几个文件来提交表单。但是表单验证器不适用于选择(付款状态)。

这是我的表格。

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。请帮我。

4

1 回答 1

2

表单的 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)));
    //...
}
于 2013-04-22T12:47:42.247 回答