views:

27

answers:

1

I'm building a simple form mailer in symfony using sfExtraForm plugin captcha. Everything works except sfValidator is not redirecting the user and showing an error when any field is invalid. The only thing I can think of is I did not generate the module. I built it from scratch.

I have to do the redirect manually. Is this because this is a sfForm and not an sfDoctrine Form? It is if the line if ($form->isValid()) returns false but without the associated redirect and error codes. Here is the program. Thanks:

Form:

<?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:*',
    ));
  }
}

Action:

    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-&gt;username.'&amp;key='.$this-&gt;key.'"&gt;here&lt;/a&gt; to finish creating your account.',$headers);
    }
    else{
        $this->redirect('/ads');
    }
  }
}
A: 

You're not seeing any errors because you're redirecting! To render form fields with their errors, you need to be rendering the actual form instance that you called bind on. Try changing $this->redirect('/ads') to $this->setTemplate('index').

Other potential changes (outside the scope of the question):

  • You can enforce the request method requirements of the send action at the routing level. Make the send route an sfRequestRoute and add the requirement { sf_method: post }.
  • Symfony has a built in mailer, sfMailer. Any reason you've decided not to use it?
jeremy
That worked. Thanks
Ite Code