我在项目中创建了一个搜索表单,但验证似乎不起作用:
Filtersearchform:
class FilterSearchForm extends sfForm
{
public function configure()
{
$def_volume = array(-1=>"Please select a volume");
$def_issue = array(-1=>"Please select issue");
$volumes = array_merge($def_volume,IssuePeer::getAllVolumesForListChoices());
$issues = array_merge($def_issue,IssuePeer::getAllIssuesForListChoices());
//******************************** WIDGET **************************************//
$this->setWidgets(array(
'keyword' => new sfWidgetFormInput(),
'volume' => new sfWidgetFormSelect(array('choices' => $volumes)),
'issue' => new sfWidgetFormSelect(array('choices' => $issues)),
));
//***************************************** VALIDATORS
**********************************//
$this->setValidators(array(
'keyword' => new sfValidatorString(array('required' => true)),
'volume' => new sfValidatorChoice(array('choices' => array_keys(IssuePeer::getAllVolumesForListChoices()))),
'issue' => new sfValidatorChoice(array('choices' => array_keys(IssuePeer::getAllIssuesForListChoices()))),
));
$this->widgetSchema->setNameFormat('filterSearch[%s]');
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
}
}
搜索操作中的:
$this->form = new FilterSearchForm();
搜索模板中的:
<form id="form_content" method="post" action="<?php echo url_for('@ResultSearch') ?>">
<?php echo $form->renderHiddenFields(); ?>
<?php if($form->hasGlobalErrors()): ?>
<ul class="error_list">
<?php foreach($form->getGlobalErrors() as $name => $error): ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<div class="search_word" >
<?php echo $form['keyword']->render();?>
<?php echo $form['keyword']->renderError() ?>
</div>
<div class="select-wrapper" data-icon="ˆ">
<?php echo $form['volume']->render();?>
<?php echo $form['volume']->renderError() ?>
</div>
<div class="select-wrapper" data-icon="ˆ" >
<?php echo $form['issue']->render();?>
<?php echo $form['issue']->renderError() ?>
</div>
<div class="search_button">
<button class="msg_submit_btn" id="fpost" type="submit"></button>
</div>
</form>
结果搜索操作中的:
if ($request->isMethod('post'))
{
$this->form = new FilterSearchForm();
$this->form->bind($request->getParameter('filterSearch'));
if ($this->form->isValid())
{
$values = $this->form->getValues();
$keyword = trim($values['keyword']);
$volume = trim($values['volume']);
$issue = trim($values['issue']);
$search= new Issue();
$object = $search->searchFilter($issue, $volume, $keyword);
$this->results= $object;
}
}
最后在ResultSearch Template:
<?php foreach ($results as $result): ?>
.....
所以例如在我的搜索tempalte中,当我将关键字字段保持为空并且我单击提交时,将我重定向到结果搜索而不显示“错误验证”==&gt; “需要关键字”。
母鸡我把var_dump我注意到代码也在Resultsearch操作中停在这里if ($this->form->isValid())
。
任何想法?
编辑:
我注意到,当我在“搜索模板”上面的代码中只使用一个页面时,这意味着post方法在同一页面<form id="form_content" method="post" action="">
中重定向,因此表单的验证工作正常,但不是我希望,我希望在提交后重定向并将搜索数据传递到上面代码中的另一个页面是“Resultsearch”模板。