我写了几个文件来提交表单。但是表单验证器不适用于选择(付款状态)。
这是我的表格。
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。请帮我。