Reputation: 1
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
Upvotes: 0
Views: 5099
Reputation: 21
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.
Upvotes: 2
Reputation: 30698
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']);
Upvotes: 1
Reputation: 160863
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;
}
Upvotes: 0
Reputation: 4881
You have to use getValues()
after you checked the form is valid. Otherwise in will return an empty array.
Upvotes: 2