private function setData(array $data, ?bool $force = null) : void
    {
        $force = $force ?? false;

        /** @var DOMElement $node */
        foreach ($this->getNodeList() as $name => $node) {
            if (! array_key_exists($name, $data)) {
                // No value set for this element
                continue;
            }

            $value = $data[$name];

            $reuseSubmittedValue = filter_var(
                $node->getAttribute('data-reuse-submitted-value'),
                FILTER_VALIDATE_BOOLEAN
            );

            if (! $reuseSubmittedValue && $force === false) {
                // Don't need to set the value
                continue;
            }

            if (in_array($node->getAttribute('type'), ['checkbox', 'radio'], true)) {
                if ($value === $node->getAttribute('value')) {
                    $node->setAttribute('checked', 'checked');
                } else {
                    $node->removeAttribute('checked');
                }
            } elseif ($node->nodeName === 'select') {
                /** @var DOMElement $option */
                foreach ($node->getElementsByTagName('option') as $option) {
                    if ($value === $option->getAttribute('value')) {
                        $option->setAttribute('selected', 'selected');
                    } else {
                        $option->removeAttribute('selected');
                    }
                }
            } elseif ($node->nodeName === 'input') {
                // Set value for input elements
                $node->setAttribute('value', (string) $value);
            } elseif ($node->nodeName === 'textarea') {
                $node->nodeValue = $value;
            }
        }
    }
private function setData(array $data, ?bool $force = null) : void
    {
        $force = $force ?? false;

        /** @var DOMElement $node */
        foreach ($this->getNodeList() as $name => $node) {
            if (! array_key_exists($name, $data)) {
                // No value set for this element
                continue;
            }

            $value = $data[$name];

            $reuseSubmittedValue = filter_var(
                $node->getAttribute('data-reuse-submitted-value'),
                FILTER_VALIDATE_BOOLEAN
            );

            if (! $reuseSubmittedValue && $force === false) {
                // Don't need to set the value
                continue;
            }

            if (in_array($node->getAttribute('type'), ['checkbox', 'radio'], true)) {
                if ($value === $node->getAttribute('value')) {
                    $node->setAttribute('checked', 'checked');
                } else {
                    $node->removeAttribute('checked');
                }
            } elseif ($node->nodeName === 'select') {
                /** @var DOMElement $option */
                foreach ($node->getElementsByTagName('option') as $option) {
                    if ($value === $option->getAttribute('value')) {
                        $option->setAttribute('selected', 'selected');
                    } else {
                        $option->removeAttribute('selected');
                    }
                }
            } elseif ($node->nodeName === 'input') {
                // Set value for input elements
                $node->setAttribute('value', (string) $value);
            } elseif ($node->nodeName === 'textarea') {
                $node->nodeValue = $value;
            }
        }
    }
private function setData ( array $ data , ? bool $ force = null ) : void { $ force = $ force ?? false ; /** @var DOMElement $node */ foreach ( $ this -> getNodeList ( ) as $ name => $ node ) { if ( ! array_key_exists ( $ name , $ data ) ) { // No value set for this element continue ; } $ value = $ data [ $ name ] ; $ reuseSubmittedValue = filter_var ( $ node -> getAttribute ( 'data-reuse-submitted-value' ) , FILTER_VALIDATE_BOOLEAN ) ; if ( ! $ reuseSubmittedValue && $ force === false ) { // Don't need to set the value continue ; } if ( in_array ( $ node -> getAttribute ( 'type' ) , [ 'checkbox' , 'radio' ] , true ) ) { if ( $ value === $ node -> getAttribute ( 'value' ) ) { $ node -> setAttribute ( 'checked' , 'checked' ) ; } else { $ node -> removeAttribute ( 'checked' ) ; } } elseif ( $ node -> nodeName === 'select' ) { /** @var DOMElement $option */ foreach ( $ node -> getElementsByTagName ( 'option' ) as $ option ) { if ( $ value === $ option -> getAttribute ( 'value' ) ) { $ option -> setAttribute ( 'selected' , 'selected' ) ; } else { $ option -> removeAttribute ( 'selected' ) ; } } } elseif ( $ node -> nodeName === 'input' ) { // Set value for input elements $ node -> setAttribute ( 'value' , ( string ) $ value ) ; } elseif ( $ node -> nodeName === 'textarea' ) { $ node -> nodeValue = $ value ; } } }
Set values and element checked and selected states
Set values and element checked and selected states