LoginSignup
0
1

More than 5 years have passed since last update.

sfWidgetFormInputCheckboxのバグ調査

Posted at

※ symfony1.4を対象としています

チェックボックスがオフにならない

symfony1.4でフォームを作成していて、チェックボックスが必要だったので設置した

$this->widgetSchema['testCheckbox'] = new sfWidgetFormInputCheckbox();

チェックボックスをonにしてPOSTすると、フォームのvalueは'on'となる

$form['testCheckbox']->getValue();
=> 'on' 

では外した時はどうなるのか?
見た目上はチェックは外れていない。。。

$form['testCheckbox']->getValue();
=> '' 

空文字列が返ってくる、が、チェックは外れない。。。

sfWidgetFormInputCheckboxのrenderメソッドを調査

widget/sfWidgetFormInputCheckbox.class.php
public function render($name, $value = null, $attributes = array(), $errors = array())
{
    if (null !== $value && $value !== false)
    {
        $attributes['checked'] = 'checked';
    }

    if (!isset($attributes['value']) && null !== $this->getOption('value_attribute_value'))
    {
        $attributes['value'] = $this->getOption('value_attribute_value');
    }

    return parent::render($name, null, $attributes, $errors);
}

renderメソッドで$valueは指定していないが、

if (null !== $value && $value !== false)
{
    $attributes['checked'] = 'checked';
}

ここでchecked属性がセットされている模様。
いろいろ調べてみると、以下のページが見つかる
http://www.devexp.eu/2009/04/23/sfwidgetforminputcheckbox-unchecked-bug/

このページを真似て、checked属性を付与している判定箇所を以下のように書き換えてみる。

if (null !== $value && $value !== false && $value !== '')
{
    $attributes['checked'] = 'checked';
}

このようにすると、renderメソッドでのcheckboxオフオン判定が正常に行われるようになる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1