0

i have some difficulties in understanding why my symfony form doesn't bind properly with the data from request...

The action:

public function executeSendEmail(sfWebRequest $request)
  {
      $history_id = $request->getParameter('id');

      if($request->isMethod(sfRequest::POST))
      {
            print_r("POST");

            $this->form = new SendEmailForm();

            $this->form->bind($request->getParameter('email_form'));
            print_r($request->getParameter('email_form'));

            if(!$this->form->isBound())
                    die('!isBound()');

            print_r($this->form->getValues());

            if($this->form->isValid())
            {
                die('form is Valid!');
            }
            die('after isValid...');
      }

      die('redirect !');

      $this->redirect('history/show?id='.$history_id);
  }

Form class:

class SendEmailForm extends sfForm
{
   public function setup()
   {
    $this->setWidgets(array(
      'author'  => new sfWidgetFormInputText(),
      'email'   => new sfWidgetFormInputText(),
      'subject' => new sfWidgetFormInputText(),
      'body'    => new sfWidgetFormTextarea(),
    ));

    $this->setValidator('email', new sfValidatorEmail());


    $this->widgetSchema->setLabels(array(
          'author'  =>  'Autor',
          'email'   =>  'E-mail',
          'subject' =>  'Tytuł',
          'body'    =>  'Treść wiadomości'
    ));

    $this->widgetSchema->setNameFormat('email_form[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
   }
}

When entering the action the $request->getParameter('email_form') contains:

Array ( 
        [author] => RRr 
        [email] => [email protected] 
        [subject] => rrrr 
        [body] => rrrr 
        [_csrf_token] => 73881c1b6217e221c4d25c065ec93052 ) 

so it look correct but nevertheless binding fails because $this->form->getValues() returns empty array() and i don't know why ;s ?! Any suggestions ? Thx in advance

4 Answers 4

2

You have to use getValues() after you checked the form is valid. Otherwise in will return an empty array.

Sign up to request clarification or add additional context in comments.

Comments

2

You can test to see what the errors are like this:

  if($this->form->hasErrors())
  {
    echo $this->form->renderGlobalErrors();
  }

If you're generating a form yourself, rather than using the Symfony helper, you might not have included / disabled the csrf token.

Comments

1

Your form class seems fine.

Try this in your action:

$this->form = new SendEmailForm();
if($request->isMethod('post')) 
{
    $this->form->bind($request->getParameter('email_form'));
    if($this->form->isValid()) 
    {
        $values = $this->form->getValues();
        var_dump($values['author']);

Comments

0

The bind() method is doing something like below, so if your form is not valid, you will get an empty array.

try
{
  $this->doBind(...);
  ...
}
catch (sfValidatorErrorSchema $e)
{
  $this->values = array(); //here
  $this->errorSchema = $e;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.