Hi,
I know it says in the book that you can create a form on the fly in an
action, but this is really bad practice.
Even if you try setting the name format for the form with "$this->form-
>widgetSchema->setNameFormat('contact[%s]')" you'll get thrown an
error because this is a protected property within the sfForm class.
Seeing as you're learning symfony and you've come across this problem,
I wanted to quickly give you an answer as I don't want you to be put
off by what the framework has to offer because of this annoying error.
To fix it:
Create a form class in your form directory (lib/form). Let's call it
ContactForm.class.php
<?php
class ContactForm extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'name' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInput(array('default' =>
'[email protected]')),
'subject' => new sfWidgetFormChoice(array('choices' =>
array('Subject A', 'Subject B', 'Subject C'))),
'message' => new sfWidgetFormTextarea(),
));
$this->setValidators(array(
'name' => new sfValidatorString(),
'email' => new sfValidatorEmail(),
'subject' => new sfValidatorString(),
'message' => new sfValidatorString(array('min_length' =>
4))
));
$this->widgetSchema->setNameFormat('contact[%s]');
}
}
In your template (if you have crsf enabled, which i hope you do
(should be default), you'll have to render it's widget):
<form action="<?php echo url_for('investigator/contact') ?>"
method="post">
<?php $form['_csrf_token'];?>
<table>
<?php echo $sf_user->getFlash('notice') ?>
<?php echo $form ?>
<tr>
<td colspan="2">
<input type="submit" value="Change" />
</td>
</tr>
</table>
</form>
In your actions, you have the following:
// Define the form
$this->form = new ContactForm();
// Deal with the request
if ($request->isMethod('post'))
{
$this->form->bind($request->getParameter('contact'));
if ($this->form->isValid())
{
// do what you want here
}
}
This form should validate correctly now.
I hope this helps and good luck with learning symfony.
Regards,
Jamie
On Dec 14, 11:17 am, gfranzini <[email protected]>
wrote:
> Hello Erkhembayar,
> Thank you.
>
> But after following your suggestion, if I try to display the form I
> get the following:
>
> Fatal error: Call to undefined method sfForm::setNameFormat() in C:\dev
> \sfproject\apps\frontend\Modules\Investigator\actions
> \contactAction.class.php on line 12
>
> On Dec 14, 2:18 am, Erkhembayar Gantulga <[email protected]> wrote:
>
> > Hi,
>
> > I think that in order to validate your form you need to put
> > $this->form->setNameFormat('contact[%s]'); for that form. After your post
> > in, $this->form->bind($request->getParameter('contact')); it doesn't have
> > correct value to validate. It would be help you.
>
> > Cheers.
> > Erkhembayar Gantulga
--
If you want to report a vulnerability issue on symfony, please send it to
security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en