Skip to content

Trim whitespace when validating required arguments in console commands #56427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2025

Conversation

eslam-dev
Copy link
Contributor

Improve Validation for Required Console Arguments

Overview

This update enhances the behavior of Artisan commands when prompting for missing required arguments. It ensures that input values consisting solely of whitespace (e.g. " ") are treated as empty, prompting the user again instead of accepting an invalid value.

Problem

Previously, when prompting for a required argument, the validation logic used:

validate: fn($value) => empty($value) ? "The {$argument->getName()} is required." : null,

This means that if a user entered only spaces, the input would pass validation because empty(" ") is false. As a result, commands could proceed with meaningless argument values.

Solution

The validation was updated to use trim() before checking for emptiness:

validate: fn($value) => empty(trim($value)) ? "The {$argument->getName()} is required." : null,

This ensures that inputs like " " are treated as empty and the user is prompted again with a proper message.

Example

Before

What is the name?
> [user enters only spaces]
# Accepted silently and assigned as a valid value.

After

What is the name?
> [user enters only spaces]
The name is required.
What is the name?
> [user enters valid string]

@taylorotwell taylorotwell merged commit 43af1f4 into laravel:12.x Jul 26, 2025
60 checks passed
@GrahamCampbell
Copy link
Member

Reverted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants