Skip to content

Conversation

@gdebrauwer
Copy link
Contributor

@gdebrauwer gdebrauwer commented Feb 6, 2026

This PR introduces whenFails and whenPasses methods on the Validator class so you can fluently react to the validation passing or failing.

This would be useful when you use the validator to validate data outside the request cycle, for example, data that is provided to a constructor or a method on a class.

// Before

public function someMethod($file)
{
	$validator = Validator::make(
		['file' => $file],
		['file' => 'required|image|dimensions:min_width=100,min_height=200']
	);
	
	if ($validator->fails()) {
		throw new InvalidArgumentException('Provided file is invalid');
	}
}
// After

public function someMethod($file)
{
	Validator::make(
		['file' => $file],
		['file' => 'required|image|dimensions:min_width=100,min_height=200']
	)->whenFails(function () {
		throw new InvalidArgumentException('Provided file is invalid');
	});
}

@gdebrauwer gdebrauwer changed the title [12.x] Add whenFails and whenPasses methods on `Validator [12.x] Add whenFails and whenPasses methods on Validator Feb 6, 2026
@taylorotwell taylorotwell merged commit 6d9ee89 into laravel:12.x Feb 6, 2026
70 checks passed
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.

2 participants