I’m trying to set a drop down to ‘required’ but the form still goes through. What am I doing wrong?
Here is the code:
this->exportForm = new sfForm();
$widgets['sheet_type'] = new sfWidgetFormDoctrineChoice(array('model' => 'ExportSheet', 'add_empty' => true));
$this->exportForm->setWidgets($widgets);
$this->exportForm->setValidators(array('sheet_type' => new sfValidatorDoctrineChoice(array('model' => 'ExportSheet', 'required' => true), array('required' => 'Please select a sheet type to export.') )));
UPDATE:
I’ve made the changes suggested.
actions.class.php:
$this->exportForm = new sfForm();
$this->exportForm->getWidgetSchema()->setNameFormat('exportForm[%s]');
$widgets['sheet_type'] = new sfWidgetFormDoctrineChoice(array('model' => 'ExportSheet', 'add_empty' => true));
$this->exportForm->setWidgets($widgets);
$this->exportForm->setValidators(array('sheet_type' => new sfValidatorDoctrineChoice(array('model' => 'ExportSheet', 'required' => true), array('required' => 'Please select a sheet type to export.') )));
if ($request->isMethod('post'))
{
$this->exportForm->bind($request->getParameter('exportForm'));
if ($this->exportForm->isValid())
{
...
}
}
Template:
<form method="post" action="<?php echo url_for('@export'); ?>">
<div id="export" style="display: block">
<?php echo $exportForm['sheet_type']->renderRow(); ?>
</form>
I can see when I submit that the echoing $request->getParameter(‘sheet_type’) does return a value. If this is the case, why is the validation still failing?
There are 2 parts to validating a form … the first is adding the validators the second is checking if its valid ….
take this example :
Its only the second part within
if ($request->isMethod('post'))that applies your validation and checks the user submitted data. See here in the symfony docs about handling forms with validationIf you use the following to add a naming schema for your form output then getting all of the results is easy ….
then you can get use :
this will then get the array with all of the field values in it and bind it to your form – ready for checking to see if its valid.
Update
you need to specify
after
as the
setWidgetsmethod resets the naming format