You are here

public function ChoiceFormField::setValue in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/dom-crawler/Field/ChoiceFormField.php \Symfony\Component\DomCrawler\Field\ChoiceFormField::setValue()

Sets the value of the field.

Parameters

string $value The value of the field:

Throws

\InvalidArgumentException When value type provided is not correct

Overrides FormField::setValue

3 calls to ChoiceFormField::setValue()
ChoiceFormField::select in vendor/symfony/dom-crawler/Field/ChoiceFormField.php
Sets the value of the field.
ChoiceFormField::tick in vendor/symfony/dom-crawler/Field/ChoiceFormField.php
Ticks a checkbox.
ChoiceFormField::untick in vendor/symfony/dom-crawler/Field/ChoiceFormField.php
Ticks a checkbox.

File

vendor/symfony/dom-crawler/Field/ChoiceFormField.php, line 116

Class

ChoiceFormField
ChoiceFormField represents a choice form field.

Namespace

Symfony\Component\DomCrawler\Field

Code

public function setValue($value) {
  if ('checkbox' === $this->type && false === $value) {

    // uncheck
    $this->value = null;
  }
  elseif ('checkbox' === $this->type && true === $value) {

    // check
    $this->value = $this->options[0]['value'];
  }
  else {
    if (is_array($value)) {
      if (!$this->multiple) {
        throw new \InvalidArgumentException(sprintf('The value for "%s" cannot be an array.', $this->name));
      }
      foreach ($value as $v) {
        if (!$this
          ->containsOption($v, $this->options)) {
          throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $v, implode(', ', $this
            ->availableOptionValues())));
        }
      }
    }
    elseif (!$this
      ->containsOption($value, $this->options)) {
      throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $value, implode(', ', $this
        ->availableOptionValues())));
    }
    if ($this->multiple) {
      $value = (array) $value;
    }
    if (is_array($value)) {
      $this->value = $value;
    }
    else {
      parent::setValue($value);
    }
  }
}