问题
I create a search form in my project but the validation seems not to be working :
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);
}
}
in search action :
$this->form = new FilterSearchForm();
in search template :
<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>
in Result search action :
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;
}
}
and Finally in ResultSearch Template :
<?php foreach ($results as $result): ?>
.....
So for example in my search tempalte when I keep the keyword field empty in form and i click submit, that redirect me to Result search without showing the "error validation" ==> "keyword required".
hen I put var_dump i remark that the code also stop here if ($this->form->isValid())
in Resultsearch action.
Any Idea?
Edit :
I remark that when I use just one page in the code above "search template",that mean the post method redirect in the same page <form id="form_content" method="post" action="">
, so the validation of the form works fine,but not that what I wish,I hope to redirect after submitting and pass the data of search to another page in the code above is "Resultsearch" template.
来源:https://stackoverflow.com/questions/16155029/validation-form-in-symfony-1-4