我正在使用 sfExtraForm 插件验证码在 symfony 中构建一个简单的表单邮件程序。一切正常,除了 sfValidator 不会重定向用户并在任何字段无效时显示错误。我唯一能想到的是我没有生成该模块。我从头开始构建它。
我必须手动进行重定向。这是因为这是 sfForm 而不是 sfDoctrine Form 吗?如果 if ($form->isValid()) 行返回 false 但没有关联的重定向和错误代码,则为这种情况。这是程序。谢谢:
形式:
<?php
class AdsForm extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'name' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInputText(),
'phone' => new sfWidgetFormInputText(),
'fax' => new sfWidgetFormInputText(),
'attention' => new sfWidgetFormInputText(),
'message' => new sfWidgetFormInputText(),
'captcha' => new sfWidgetFormReCaptcha( array('public_key'=>self::PUBLIC_KEY)),
));
$this->widgetSchema->setNameFormat('ads[%s]');
$this->setValidators(array(
'name' => new sfValidatorString(array('max_length' => 50)),
'email' => new sfValidatorString(array('max_length' => 50)),
'phone' => new sfValidatorString(array('max_length' => 50)),
'fax' => new sfValidatorString(array('max_length' => 50)),
'attention' => new sfValidatorString(array('max_length' => 50)),
'message' => new sfValidatorString(array('max_length' => 255)),
'captcha' => new sfValidatorReCaptcha(array('private_key' => self::PRIVATE_KEY))
));
$this->widgetSchema->setLabels(array(
'name' => 'Your Name:*',
'email' => 'Your Email:*',
'phone' => 'Your Phone:*',
'fax' => 'Your Fax',
'attention' => 'Attention:*',
'message' => 'Mail Message:*',
'captcha' => 'Image Verification:*',
));
}
}
行动:
class adsActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
$this->form=new adsForm();
}
public function executeSend(sfWebRequest $request){
$this->forward404Unless($request->isMethod(sfRequest::POST));
$form=new adsForm();
$captcha = array('recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'),);
$form->bind(array_merge($request->getParameter('ads'), array('captcha' => $captcha)));
if ($form->isValid())
{
print_r($_REQUEST);
$ads=$this->getRequestParameter('ads');
print_r($ads);
$headers = 'From: '."\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//mail(' ******@yahoo.com','Yellow Explorer','An account has been created for you at yellowexplorer.com with a username of '.$this->username.'. Please click <a href="http://yellowexplorer.itecode.com/user/ConfirmRegistration?username='.$this->username.'&key='.$this->key.'">here</a> to finish creating your account.',$headers);
}
else{
$this->redirect('/ads');
}
}
}
您没有看到任何错误,因为您正在重定向!要呈现带有错误的表单字段,您需要呈现您调用绑定的实际表单实例。尝试将
$this->redirect('/ads')
更改为 $this->setTemplate('index')
。
其他潜在的变化(超出问题范围):
sfRequestRoute
并添加要求 { sf_method: post }
。sfMailer
。您决定不使用它有什么原因吗?