Search code examples
formsvalidationcheckboxsymfony-1.4

Symfony 1.4, sfWidgetFormInputCheckbox and sfValidatorBoolean. Value is "on" when checkbox is checked


Hi happy dev' people :)

I'm facing a very strange problem with that old and deprecated Symfony 1.4. To explain it 'quick & simple' i have a checkbox made with sfWidgetFormInputCheckbox() and a validator for that widget made with sfValidatorBoolean(), normal tools for doing this in Symfony so. But when i check the checkbox and submit, the value is on!! Not 1, not true but on!! I don't need to start an engine with on off values :) Anyway i give now all useful code and if you find something, let me know because it's driving me crazy! This is my 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';
 }

}

My 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.");
  }
}

The part of code in 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);

And this what firebug give to me when i submit my form.

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'??

I precise that in database the field terms_of_use_acceptance is a TINYIT where you can put 1 or 0 (like true or false). Everything is working with other field on other pages with that TINYIT working like Boolean but that one won't work... Hope i was clear enough.

P.S: The answer 'Update to Symfony 2' IS NOT VALID! I can't do this because it is not a personal project. If it was up to me, it would be already done!


Solution

  • Ok i had these lines for validation.

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

    I've change to this and now it's working.

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