Symfony\Component\DomCrawler\Crawler::form PHP Method

form() public method

Returns a Form object for the first node in the list.
public form ( array $values = null, string $method = null ) : Form
$values array An array of values for the form fields
$method string The method for the form
return Form A Form instance
    public function form(array $values = null, $method = null)
    {
        if (!$this->nodes) {
            throw new \InvalidArgumentException('The current node list is empty.');
        }
        $node = $this->getNode(0);
        if (!$node instanceof \DOMElement) {
            throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node)));
        }
        $form = new Form($node, $this->uri, $method, $this->baseHref);
        if (null !== $values) {
            $form->setValues($values);
        }
        return $form;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Returns a crawler Form object for the form pointed to by the
  * passed Crawler.
  *
  * The returned form is an independent Crawler created to take care
  * of the following issues currently experienced by Crawler's form
  * object:
  *  - input fields disabled at a higher level (e.g. by a surrounding
  *    fieldset) still return values
  *  - Codeception expects an empty value to match an unselected
  *    select box.
  *
  * The function clones the crawler's node and creates a new crawler
  * because it destroys or adds to the DOM for the form to achieve
  * the desired functionality.  Other functions simply querying the
  * DOM wouldn't expect them.
  *
  * @param Crawler $form the form
  * @param string $action the form's absolute URL action
  * @return Form
  */
 private function getFormFromCrawler(Crawler $form, $action)
 {
     $fakeDom = new \DOMDocument();
     $fakeDom->appendChild($fakeDom->importNode($form->getNode(0), true));
     $node = $fakeDom->documentElement;
     $cloned = new Crawler($node, $action);
     $shouldDisable = $cloned->filter('input:disabled:not([disabled]),select option:disabled,select optgroup:disabled option:not([disabled])');
     foreach ($shouldDisable as $field) {
         $field->parentNode->removeChild($field);
     }
     $selectNonMulti = $cloned->filterXPath('//select[not(@multiple) and not(option[@value=""])]');
     $opt = new \DOMElement('option');
     foreach ($selectNonMulti as $field) {
         $node = $field->insertBefore($opt, $field->firstChild);
         $node->setAttribute('value', '');
     }
     return $cloned->form();
 }
All Usage Examples Of Symfony\Component\DomCrawler\Crawler::form