Skip to content

Add Stringable interface support in Validator.php - #349

Open
spaze wants to merge 13 commits into
nette:v3.2from
spaze:spaze/validator-stringable
Open

Add Stringable interface support in Validator.php#349
spaze wants to merge 13 commits into
nette:v3.2from
spaze:spaze/validator-stringable

Conversation

@spaze

@spaze spaze commented Feb 22, 2026

Copy link
Copy Markdown
Contributor
  • bug fix / new feature? bugfix
  • BC break? no

The addRule methods support Stringable for $errorMessage:

	public function addRule(
		callable|string $validator,
		string|Stringable|null $errorMessage = null,
		mixed $arg = null,
	) {

but when I pass an object of class MyRule implements Stringable, I'll get a TypeError:

preg_replace_callback(): Argument #3 ($subject) must be of type array|string, MyRule given

That's because Nette\Forms\Validator only supports Nette\HtmlStringable:
https://github.com/nette/forms/blob/ae11e90098c39576d8744d6bacc35dd858ed4167/src/Forms/Validator.php#L57C33-L58
so an Stringable object will reach preg_replace_callback() and it doesn't know what to do with it.

There are several options here. The first is to return the Stringable object type-casted to string, which is what I've implemented in this PR, another would be to type-cast $message here:

}, $message);

Let me know if I should change the PR to do that.

@dg
dg force-pushed the v3.2 branch 4 times, most recently from 8da14c7 to 8c59b75 Compare June 28, 2026 12:31
@dg
dg force-pushed the v3.2 branch 2 times, most recently from 4fd678c to 0c7ada6 Compare June 29, 2026 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants