Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ChoiceFormField.php

function ChoiceFormField::setValue

Sets 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
Unticks a checkbox.

File

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

Class

ChoiceFormField
ChoiceFormField represents a choice form field.

Namespace

Symfony\Component\DomCrawler\Field

Code

public function setValue(string|array|bool|null $value) : void {
    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);
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal