4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler\Field;
15 * FormField is the abstract class for all form fields.
17 * @author Fabien Potencier <fabien@symfony.com>
19 abstract class FormField
47 * @param \DOMElement $node The node associated with this field
49 public function __construct(\DOMElement $node)
52 $this->name = $node->getAttribute('name');
53 $this->xpath = new \DOMXPath($node->ownerDocument);
59 * Returns the label tag associated to the field or null if none.
61 * @return \DOMElement|null
63 public function getLabel()
65 $xpath = new \DOMXPath($this->node->ownerDocument);
67 if ($this->node->hasAttribute('id')) {
68 $labels = $xpath->query(sprintf('descendant::label[@for="%s"]', $this->node->getAttribute('id')));
69 if ($labels->length > 0) {
70 return $labels->item(0);
74 $labels = $xpath->query('ancestor::label[1]', $this->node);
75 if ($labels->length > 0) {
76 return $labels->item(0);
81 * Returns the name of the field.
83 * @return string The name of the field
85 public function getName()
91 * Gets the value of the field.
93 * @return string|array The value of the field
95 public function getValue()
101 * Sets the value of the field.
103 * @param string $value The value of the field
105 public function setValue($value)
107 $this->value = (string) $value;
111 * Returns true if the field should be included in the submitted values.
113 * @return bool true if the field should be included in the submitted values, false otherwise
115 public function hasValue()
121 * Check if the current field is disabled.
125 public function isDisabled()
127 return $this->node->hasAttribute('disabled');
131 * Initializes the form field.
133 abstract protected function initialize();