プロジェクト

全般

プロフィール

Backport(バックポート) #3810

sfValidatorChoice に数値以外を渡した場合に正しくバリデーションが行われない場合がある

Chiharu Nakajimaほぼ9年前に追加. 8年以上前に更新.

ステータス:
Fixed(完了)
優先度:
High(高め)
対象バージョン:
開始日:
2013-09-12
期日:
進捗率:

100%


説明

概要

sfValidatorChoice に数値以外を渡した場合に正しくバリデーションが行われない場合がある.

sfValidatorChoice に choices オプションを渡すと渡された配列に含まれるかどうかについてバリデーションが行われるが,この choices に指定される配列の中身に "00" や "11.1" といったような文字列が渡された場合に正しくバリデーションが行われれない.

この問題については下記チケットで reopen されている.ただし動きはない.

#4212 (sfValidatorChoice in_array bug (php bug?)) - symfony - Trac
http://trac.symfony-project.org/ticket/4212

原因

chices に含まれるかどうかの判定で string にキャストした後に == でチェックしているため,文字列に数値が含まれる場合に正しく評価されない.

lib/vendor/symfony/lib/validator/sfValidatorChoice.class.php
117   /** 
118    * Checks if a value is part of given choices (see bug #4212) 
119    * 
120    * @param  mixed $value   The value to check 
121    * @param  array $choices The array of available choices 
122    * 
123    * @return Boolean 
124    */ 
125   static protected function inChoices($value, array $choices = array()) 
126   { 
127     foreach ($choices as $choice) 
128     { 
129       if ((string) $choice == (string) $value) 
130       { 
131         return true; 
132       } 
133     } 
134  
135     return false; 
136   } 

修正案

sfValidatorChoice 自体は choices に整数値を与えることを目的としたバリデータとしたうえで, choices に文字列など整数値以外のものを対象とした opValidatorChoice のようなクラスを新たにつくり, inChoices および inChoices を用いた部分を上書きする. inChoices 自体が static で sfValidatorChoice で self 呼び出ししているため継承してオーバーライドするだけでは有効に働かない様子.

テストコード

<?php

include_once dirname(__FILE__) . '/../../bootstrap/unit.php';

$t = new lime_test(9, new lime_output_color());

$t->diag('opValidatorChoice');

$t->diag('->clean()');

$v = new opValidatorChoice(array(
  'choices' => array('00', '000', '0.0'),
));

try
{
  $v->clean('0');
  $t->fail('->clean() throws a sfValidatorError if not in choices');
  $t->skip('', 1);
}
catch (Exception $e)
{
  $t->pass('->clean() throws a sfValidatorError if not in choices');
  $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
}

$t->is($v->clean('00'), '00', '->clean() accepts in choices');
$t->is($v->clean('000'), '000', '->clean() accepts in choices');
$t->is($v->clean('0.0'), '0.0', '->clean() accepts in choices');

$v = new opValidatorChoice(array(
  'choices' => array('011', '11.0'),
));

try
{
  $v->clean('11');
  $t->fail('->clean() throws a sfValidatorError if not in choices');
  $t->skip('', 1);
}
catch (Exception $e)
{ 
  $t->pass('->clean() throws a sfValidatorError if not in choices');
  $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
}

$t->is($v->clean('011'), '011', '->clean() accepts in choices');
$t->is($v->clean('11.0'), '11.0', '->clean() accepts in choices');

t-2982.patch 表示 (3.52 KB) Shinichi Urabe, 2015-06-19 16:43


関連するチケット

コピー元 OpenPNE 3 - Bug(バグ) #3401: sfValidatorChoice に数値以外を渡した場合に正しくバリデーションが行われない場合がある Won't fix(対応せず) 2013-09-12

関係しているリビジョン

リビジョン 36cbc947 (差分)
Chiharu Nakajima8年以上前に追加

(refs #3810, BP from #3401) Bug Fixed a validation of sfValidatorChoice

リビジョン 000c44c3
Shinichi Urabe8年以上前に追加

Merge pull request #256 from C-nakajima/t-3810

(refs #3810, BP from #3401) Bug Fixed a validation of sfValidatorChoice

履歴

#1 Chiharu Nakajimaほぼ9年前に更新

  • コピー元 Bug(バグ) #3401: sfValidatorChoice に数値以外を渡した場合に正しくバリデーションが行われない場合がある を追加

#2 Chiharu Nakajima8年以上前に更新

  • ステータスNew(新規) から Pending Review(レビュー待ち) に変更
  • 進捗率0 から 50 に変更

プルリクエストしました。
https://github.com/openpne/OpenPNE3/pull/256

#3 Shinichi Urabe8年以上前に更新

  • 担当者Chiharu Nakajima にセット

#4 Shinichi Urabe8年以上前に更新

  • ステータスPending Review(レビュー待ち) から Pending Testing(テスト待ち) に変更
  • 進捗率50 から 70 に変更

#5 Chiharu Nakajima8年以上前に更新

  • ステータスPending Testing(テスト待ち) から Pending Merge(マージ待ち) に変更
  • 進捗率70 から 80 に変更

試験実施済み

#6 Shinichi Urabe8年以上前に更新

  • ステータスPending Merge(マージ待ち) から Fixed(完了) に変更
  • 進捗率80 から 100 に変更

merged

他の形式にエクスポート: Atom PDF