【问题标题】:Symfony 1.4, sfWidgetFormInputCheckbox and sfValidatorBoolean. Value is "on" when checkbox is checkedSymfony 1.4,sfWidgetFormInputCheckbox 和 sfValidatorBoolean。选中复选框时值为“on”
【发布时间】:2015-09-15 08:16:49
【问题描述】:

嗨,快乐的开发人员 :)

我在使用旧的和已弃用的 Symfony 1.4 时遇到了一个非常奇怪的问题。 为了“快速简单”地解释它,我有一个用sfWidgetFormInputCheckbox() 制作的复选框和一个用sfValidatorBoolean() 制作的小部件的验证器,这是在Symfony 中执行此操作的常规工具。 但是当我选中复选框并提交时,值为on!! 不是1,不是true,而是on!!我不需要用on off 值启动引擎:) 无论如何,我现在给出所有有用的代码,如果你发现了什么,请告诉我,因为它让我发疯! 这是我的BaseHomeUserAcceptedCguForm.class.php

abstract class BaseHomeUserAcceptedCguForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                      => new sfWidgetFormInputHidden(),
      'terms_of_use_acceptance' => new sfWidgetFormInputCheckbox(),
      'letter_sort_id'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('LetterSort'), 'add_empty' => true)),
      'home_user_id'            => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('HomeUser'), 'add_empty' => true)),
    ));

   $this->setValidators(array(
     'id'                      => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
     'terms_of_use_acceptance' => new sfValidatorBoolean(array('required' => false)),
     'letter_sort_id'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('LetterSort'), 'required' => false)),
     'home_user_id'            => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('HomeUser'), 'required' => false)),
   ));

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

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

   $this->setupInheritance();

   parent::setup();
 }

 public function getModelName()
 {
   return 'HomeUserAcceptedCgu';
 }

}

我的HomeUserAcceptedCguForm.class.php

class HomeUserAcceptedCguForm extends BaseHomeUserAcceptedCguForm
{
    public $fields = array(
    'terms_of_use_acceptance',
    'home_user_id',
    'letter_sort_id'
  );

  public function configure()
  {
    parent::configure();

    $this->widgetSchema['home_user_id'] = new sfWidgetFormInputHidden();
    $this->widgetSchema['letter_sort_id'] = new sfWidgetFormInputHidden();

    $this->validatorSchema['terms_of_use_acceptance'] = new sfValidatorBoolean(array('required' => true));
    $this->validatorSchema['terms_of_use_acceptance']->setMessage('required', "Vous devez accepter les conditions d'utilisation du service pour effectuer cette prestation.");
  }
}

action.class.php中的代码部分

$this->newCGUForm = new HomeUserAcceptedCguForm();
$this->newCGUForm->getWidgetSchema()->setFormFormatterName('div');
$this->newCGUForm->setDefault('home_user_id', $home_user_id[0]["id"]);
$this->newCGUForm->setDefault('letter_sort_id', $letter_sort_id);
$this->newCGUForm->setDefault('terms_of_use_acceptance', false);

这就是我提交表单时萤火虫给我的东西。

home_user_accepted_cgu[home_user_id]    8193 //good value
home_user_accepted_cgu[letter_sort_id]  1022 //good value
home_user_accepted_cgu[terms_of_use_acceptance] on //the wrong value! Where is picking that 'on'??

我确切地说,在数据库中,terms_of_use_acceptance 字段是一个 TINYIT,您可以在其中放置10(如真或假)。一切都与其他页面上的其他领域一起工作,TINYIT 像布尔一样工作,但那个不起作用......希望我足够清楚。

P.S:“更新到 Symfony 2”的答案无效!我不能这样做,因为这不是个人项目。如果由我来决定,那就已经完成了!

【问题讨论】:

  • sfValidatorBoolean 将 'on' 转换为 boolean true。在我看来,您使用的表单不​​正确。

标签: forms validation checkbox symfony-1.4


【解决方案1】:

好的,我有这些行用于验证。

if ($this->newCGUForm->isValid())  {
  $this->newCGUForm->save();
}

我已经改变了这个,现在它可以工作了。

if ($this->newCGUForm->isValid())  {
  $this->newCGUForm->save();
  $newCGU = $this->newCGUForm->getObject();
  $newCGU->save();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2021-04-07
    • 1970-01-01
    • 2017-11-22
    • 2011-09-11
    • 1970-01-01
    相关资源
    最近更新 更多