Symfony 1.4 表单绑定问题
我已经浏览了一段时间的答案,并在这里提出了一个问题。我有几个可用的 sf 表单,但由于某种原因,这个表单不想这样做。我有一种感觉,我错过了一个小细节,我过去很幸运地忽略了这一点,没有出现任何错误。
这是架构:
public function configure()
{
//widgets
$this->setWidgets(array(
'study_area' => new sfWidgetFormSelect( array( 'label' => 'Select Study Area', 'choices' => self::study_areas() )),
'degree_level' => new sfWidgetFormSelect( array( 'label' => 'Select Degree Level', 'choices' => self::degree_levels() )),
'campusonline' => new sfWidgetFormSelectRadio( array( 'choices' => self::campus_online(), 'class' => 'radiolabel' )),
'zip' => new sfWidgetFormInputText( array( 'label' => 'Zip code:')),
));
//validators
$this->setValidators(array(
'study_area' => new sfValidatorString( array( 'required' => true )),
'degree_level' => new sfValidatorString( array( 'required' => true )),
'campusonline' => new sfValidatorString( array( 'required' => true )),
'zip' => new sfValidatorString(array( 'required' => 'Please enter a valid zip.' )),
));
//formatting, default values, name format, etc
$this->getWidgetSchema()->getFormFormatter()->setRowFormat( '%label%%error%%field%%help%%hidden_fields%' );
$this->setDefaults( array( 'study_area' => '', 'degree_level' => '', 'campusonline' => 1 ));
$this->getWidgetSchema()->setLabel( 'campusonline', false );
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
$this->validatorSchema['zip'] = new sfValidatorZip(array('required' => true));
}
这是操作:
$this->form = new SearchWidgetForm();
if ($request->isMethod( 'post' ))
{
$this->form->bind($request->getParameter( 'searchwidget' ));
if ($this->form->isValid())
{
$this->redirect('@search_widget');
}
else
{
print 'nope';
$this->form->debug();
var_dump($request->getParameter( 'searchwidget' ));
die;
}
}
这是 else
的结果:
nope
study_area: Required.
degree_level: Required.
campusonline: Required.
zip: Required.
_csrf_token: Required.
null
我还检查了 var_dump( $this->form->isBound() ) ;
产生“true”,它确实如此。所以...表单是绑定的,但它应该变成的数组是空的。
有什么想法吗?
预先感谢您的任何帮助。我真的很感激。
I've been browsing for answers for a while and I've given in to asking a question here. I have several working sf forms, but for some reason this one doesn't want to do it. I have a feeling I am missing a small detail I've been lucky to overlook without errors in the past.
Here is the schema:
public function configure()
{
//widgets
$this->setWidgets(array(
'study_area' => new sfWidgetFormSelect( array( 'label' => 'Select Study Area', 'choices' => self::study_areas() )),
'degree_level' => new sfWidgetFormSelect( array( 'label' => 'Select Degree Level', 'choices' => self::degree_levels() )),
'campusonline' => new sfWidgetFormSelectRadio( array( 'choices' => self::campus_online(), 'class' => 'radiolabel' )),
'zip' => new sfWidgetFormInputText( array( 'label' => 'Zip code:')),
));
//validators
$this->setValidators(array(
'study_area' => new sfValidatorString( array( 'required' => true )),
'degree_level' => new sfValidatorString( array( 'required' => true )),
'campusonline' => new sfValidatorString( array( 'required' => true )),
'zip' => new sfValidatorString(array( 'required' => 'Please enter a valid zip.' )),
));
//formatting, default values, name format, etc
$this->getWidgetSchema()->getFormFormatter()->setRowFormat( '%label%%error%%field%%help%%hidden_fields%' );
$this->setDefaults( array( 'study_area' => '', 'degree_level' => '', 'campusonline' => 1 ));
$this->getWidgetSchema()->setLabel( 'campusonline', false );
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
$this->validatorSchema['zip'] = new sfValidatorZip(array('required' => true));
}
And here is the action:
$this->form = new SearchWidgetForm();
if ($request->isMethod( 'post' ))
{
$this->form->bind($request->getParameter( 'searchwidget' ));
if ($this->form->isValid())
{
$this->redirect('@search_widget');
}
else
{
print 'nope';
$this->form->debug();
var_dump($request->getParameter( 'searchwidget' ));
die;
}
}
And here is what results from that else
:
nope
study_area: Required.
degree_level: Required.
campusonline: Required.
zip: Required.
_csrf_token: Required.
null
I have also checked that var_dump( $this->form->isBound() );
produces 'true', which it does. So... The form is bound, but the array it should have become is null.
Any ideas?
Thanks in advance for any help. I would really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是在这里猜测(暂时没有使用过 sf1.x),但也许您需要设置表单名称格式?我看到您在第一个代码片段的最后一条评论中提到了它,但我没有看到实际的行:
I'm only guessing here, (haven't used sf1.x in a bit) but maybe you need to set the form name format? I see you mentioned it in the last comment in your first code snippet, but I don't see the actual line: