我在我的项目中创建了一个搜索表单,但验证似乎不起作用:
过滤搜索表单:
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 模板中:
<?php foreach ($results as $result): ?>
.....
因此,例如,在我的搜索模板中,当我将表单中的关键字字段保持为空并单击提交时,会将我重定向到结果搜索而不显示“错误验证”==>“需要关键字”。
当我放 var_dump 时,我说代码也在if ($this->form->isValid())
Resultsearch 操作中停止。
任何想法?
编辑 :
我注意到当我在“搜索模板”上面的代码中只使用一个页面时,这意味着 post 方法在同一页面中重定向<form id="form_content" method="post" action="">
,所以表单的验证工作正常,但不是我想要的,我希望在之后重定向上述代码中的“Resultsearch”模板提交并传递搜索的数据到另一个页面。