Form->addRule : Constants not working: What is my mistake? OR Docs not up-to-date?

Nicolas_K
Member | 25
+
0
-

1. This is my form in my HomepagePresenter:

namespace App\Presenters;
	use App\Model\PageFacade;
	use Nette;
	use Nette\Application\UI\Form;
	use Nette\Caching\Cache;
	use Nette\Utils\Arrays;
	...
	$form = new Form;
	$form->addText('form_name', 'Name*:')
		->setRequired('Dein Name')
		->setHtmlAttribute('placeholder', 'Dein Name')
		->addRule($form::Range, 'Mind. %d, max. %d Buchstaben', [2, 40])
		;
	$form->addSubmit('submit', 'Abschicken')
		->setOmitted();

According to https://doc.nette.org/…in-presenter#… :
(copied from the doc)

$form->addInteger('age', 'Age:')
	->addRule($form::Range, 'You must be older 18 years and be under 120.', [18, 120]);
$form->addPassword('password', 'Password:')
	->setRequired('Pick a password')
	->addRule($form::MinLength, 'Your password has to be at least %d long', 8);

2. This is the result – given by Tracy:

Undefined constant Nette\Application\UI\Form::Range

The same with other constants like MinLength, MaxLength, Pattern.

According to https://doc.nette.org/…s/validation#…
and https://api.nette.org/…ms/Form.html
these constants should work (?)

3. When I do it this way, it works!

	$form = new Form;
	$form->addText('form_name', 'Name*:')
		->setRequired('Dein Name')
		->setHtmlAttribute('placeholder', 'Dein Name')
		->setHtmlAttribute('autocomplete', 'on')
		->addRule($form::RANGE, 'Mind. %d, max. %d Buchstaben', [2, 40])
		->addRule($form::PATTERN, 'Einen normalen Namen, bitte', '([a-zA-Z]{2,})([-a-zA-Z\.\s]*)')
		;

4. This my actual composer.json

{
	"name": "nette/web-project",
	"description": "Test: Web Project => flat-file .md",
	"type": "project",
	"license": "none",
	"minimum-stability": "stable",
	"require": {
		"php": ">=8.1 <8.3",
		"nette/application": "^3.1",
		"nette/bootstrap": "^3.2",
		"nette/caching": "^3.2",
		"nette/di": "^3.1",
		"nette/forms": "^3.1",
		"nette/http": "^3.2",
		"nette/mail": "^4.0",
		"nette/neon": "^3.4",
		"nette/robot-loader": "^4.0",
		"nette/security": "^3.1",
		"nette/utils": "^4.0",
		"latte/latte": "^3.0",
		"tracy/tracy": "^2.9",
		"erusev/parsedown": "^1.7"
	},
	"require-dev": {
		"nette/tester": "^2.4",
		"phpstan/phpstan-nette": "^1.2"
	},
	"autoload": {
		"psr-4": {
			"App\\": "app"
		}
	}
}
Rick Strafy
Nette Blogger | 61
+
0
-

Hi, you need to select correct version in the docs, for some reason 4.0 is default, 3.x is here https://doc.nette.org/…lidation/3.x.

Lumeriol
Generous Backer | 49
+
0
-

Nicolas_K wrote:

1. This is my form in my HomepagePresenter:

You must use constants with uppercases, like $form::RANGE, $form::PATTERN etc.

Constants as PascalCase are defined in Nette\Forms 4.0, that is not fully released, but documentation is ready for that. It is reason, why 3rd point is fully works.

Nicolas_K
Member | 25
+
0
-

Then it's clear.. Thanks a lot!