Project

General

Profile

t-2982.patch

Shinichi Urabe, 2015-06-19 16:43

Download (3.52 KB)

View differences:

lib/validator/opValidatorChoice.class.php
1
<?php
2

  
3
class opValidatorChoice extends sfValidatorChoice
4
{
5
  /**
6
   * @see sfValidatorBase
7
   */
8
  protected function doClean($value)
9
  {
10
    $choices = $this->getChoices();
11

  
12
    if ($this->getOption('multiple'))
13
    {
14
      $value = $this->cleanMultiple($value, $choices);
15
    }
16
    else
17
    {
18
      if (!self::inChoices($value, $choices))
19
      {
20
        throw new sfValidatorError($this, 'invalid', array('value' => $value));
21
      }
22
    }
23

  
24
    return $value;
25
  }
26

  
27
  /**
28
   * Cleans a value when multiple is true.
29
   *
30
   * @param  mixed $value The submitted value
31
   *
32
   * @return array The cleaned value
33
   */
34
  protected function cleanMultiple($value, $choices)
35
  {
36
    if (!is_array($value))
37
    {
38
      $value = array($value);
39
    }
40

  
41
    foreach ($value as $v)
42
    {
43
      if (!self::inChoices($v, $choices))
44
      {
45
        throw new sfValidatorError($this, 'invalid', array('value' => $v));
46
      }
47
    }
48

  
49
    $count = count($value);
50

  
51
    if ($this->hasOption('min') && $count < $this->getOption('min'))
52
    {
53
      throw new sfValidatorError($this, 'min', array('count' => $count, 'min' => $this->getOption('min')));
54
    }
55

  
56
    if ($this->hasOption('max') && $count > $this->getOption('max'))
57
    {
58
      throw new sfValidatorError($this, 'max', array('count' => $count, 'max' => $this->getOption('max')));
59
    }
60

  
61
    return $value;
62
  }
63

  
64
  /**
65
   * Checks if a value is part of given choices (see bug #4212)
66
   *
67
   * @param  mixed $value   The value to check
68
   * @param  array $choices The array of available choices
69
   *
70
   * @return Boolean
71
   */
72
  static protected function inChoices($value, array $choices = array())
73
  {
74
    foreach ($choices as $choice)
75
    {
76
      if ((string) $choice === (string) $value)
77
      {
78
        return true;
79
      }
80
    }
81

  
82
    return false;
83
  }
84
}
test/unit/validator/opValidatorChoiceTest.php
1
<?php
2

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

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

  
7
$t->diag('opValidatorChoice');
8

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

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

  
15
try
16
{
17
  $v->clean('0');
18
  $t->fail('->clean() throws a sfValidatorError if not in choices');
19
  $t->skip('', 1);
20
}
21
catch (Exception $e)
22
{
23
  $t->pass('->clean() throws a sfValidatorError if not in choices');
24
  $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
25
}
26

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

  
31

  
32
$v = new opValidatorChoice(array(
33
  'choices' => array('011', '11.0'),
34
));
35

  
36
try
37
{
38
  $v->clean('11');
39
  $t->fail('->clean() throws a sfValidatorError if not in choices');
40
  $t->skip('', 1);
41
}
42
catch (Exception $e)
43
{
44
  $t->pass('->clean() throws a sfValidatorError if not in choices');
45
  $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
46
}
47

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