Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DomCrawler][BrowserKit] Unable to submit form with button value #58509

Copy link
Copy link
Open
@JorickPepin

Description

@JorickPepin
Issue body actions

Symfony version(s) affected

7.1

Description

The submitForm(string $button, ...) method indicates :

@param string $button The text content, id, value or name of the form <button> or <input type="submit">

However, the value attribute doesn't seem to be checked :

Symfony\Component\BrowserKit\Exception\InvalidArgumentException: There is no button with "btn_value" as its content, id, value or name.

How to reproduce

<form method="post">
    <input type="text" id="name" name="name" required>

    <button type="submit" id="btn_id" name="btn_name" value="btn_value">My button</button>
</form>
public function testFormButton(): void
{
    $client = static::createClient();

    $client->request('GET', '/');

    $client->submitForm('My button', ['name' => 'John']);
    $client->submitForm('btn_id', ['name' => 'John']);
    $client->submitForm('btn_name', ['name' => 'John']);
    $client->submitForm('btn_value', ['name' => 'John']); // this fails
}

Possible Solution

The XPath query behind this doesn't check the value attribute of <button> tags:

// Crawler.php

public function selectButton(string $value): static
{
    return $this->filterRelativeXPath(
        sprintf('descendant-or-self::input[((contains(%1$s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)) or (contains(%1$s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)) or @id=%3$s or @name=%3$s] | descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s) or @id=%3$s or @name=%3$s]', 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")', static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value))
    );
}

I can work on a PR if you confirm the issue. Thanks

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.