Skip to content

Comments

Add Stringable interface support in Validator.php#349

Open
spaze wants to merge 1 commit intonette:v3.2from
spaze:spaze/validator-stringable
Open

Add Stringable interface support in Validator.php#349
spaze wants to merge 1 commit intonette:v3.2from
spaze:spaze/validator-stringable

Conversation

@spaze
Copy link
Contributor

@spaze spaze commented Feb 22, 2026

  • 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.

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.

1 participant