symfony验证码错误重定向

时间:2010-09-18 00:45:10

标签: symfony1 doctrine

我正在使用sfExtraForm插件验证码在symfony中构建一个简单的表单邮件程序。一切正常,除了sfValidator没有重定向用户并在任何字段无效时显示错误。我唯一能想到的是我没有生成模块。我从头开始构建它。

我必须手动进行重定向。这是因为这是一个sfForm而不是sfDoctrine表单吗?如果行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: no-reply@yellowexplorer.com' . "\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');
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您没有看到任何错误,因为您正在重定向!要使用错误呈现表单字段,您需要呈现您调用bind的实际表单实例。尝试将$this->redirect('/ads')更改为$this->setTemplate('index')

其他可能的变化(问题范围之外):

  • 您可以在路由级别强制执行发送操作的请求方法要求。将发送路由设为sfRequestRoute并添加要求{ sf_method: post }
  • Symfony有一个内置邮件程序sfMailer。你有什么理由决定不使用它吗?