Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/validator/sfValidatorChoice.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ protected function isEmpty($value)
*/
static protected function inChoices($value, array $choices = array())
{
if (is_array($value) && 1 === count($value)) {
$value = reset($value);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please shortly explain why is it needed and why is it php7 related?
Is (string)$array works differently in php7?
How it supposed to work in $value is an array but with more them 1 element?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not really php7 related, did popup before as well. The Choice validator casts choice values to strings before checking if they exist in $choices array, but the values can be arrays as well (here a sfWidgetFormSelectRadio widget). To prevent the warning/notification the value array should be replaced by its only item

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a misuse of the validator - because it has other clean function for the case when multiple choice is enabled

  protected function doClean($value)
  {
    $choices = $this->getChoices();

    if ($this->getOption('multiple'))
    {
      $value = $this->cleanMultiple($value, $choices);
    }
    else
    {
      if (!self::inChoices($value, $choices))
      {
        throw new sfValidatorError($this, 'invalid', array('value' => $value));
      }
    }

    return $value;
  }

So somewhere "multiple" widget is used with choice validator where "multiple" option is not enabled. We rather fix that one, than making this change.

foreach ($choices as $choice)
{
if ((string) $choice == (string) $value)
Expand Down