Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] ChoiceQuestion removes spaces from user input #32502

Closed
@IceMaD

Description

@IceMaD

Symfony version(s) affected: all

Description
When using ChoiceQuestion with multiselect option, the defaultValidator removes spaces from the input which result in an error "Value [input] is invalid"

How to reproduce

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;

class DemoCommand extends Command
{
    protected static $defaultName = 'demo';

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);

        $io->askQuestion(
            (new ChoiceQuestion('Question', [
                'First response',
                'Second response',
            ]))
                ->setMultiselect(true)
        );
    }
}

Possible Solution
Removing the str_replace from de default validator and using trim on the result of explode instead.
I already prepared a fix with tests. The pull request is coming

Additional context

demo

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