Search code examples
phplaravel-5laravel-dusk

How to type in Laravel Dusk with an array of input text?


I have these array of input text :

<input type="text" name="amount[0]">
<input type="text" name="amount[1]">
<input type="text" name="amount[2]">
<input type="text" name="amount[3]">

and I want to type a value using Laravel Dusk. I can't just hardcode using :

$browser->visit('/create')
    ->type('amount[0]', '100')
    ->type('amount[1]', '100')
    ->type('amount[2]', '100')
    ->type('amount[3]', '100');

because the number of input fields depends on the number of items in the database.

I tried using :

$browser->visit('/create')
    ->type('amount[]', '100');

but it doesn't work. Is there a way to achieve this?


Solution

  • You can find the inputs with a CSS selector:

    $inputs = $browser->elements('input[name^="amount["]');
    
    foreach ($inputs as $input) {
        $input->sendKeys('100');
    }