From 51840cfc019bc57d3a19892e55800355f11cbba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Brecher?= <34324008+mildabre@users.noreply.github.com> Date: Tue, 30 May 2023 23:23:53 +0200 Subject: [PATCH] References Between Controls - more clear description, better code sample addRule() with reference to another form input is in practice mainly usable in case of comparing equality of two text inputs - input of password or email or another important data for better control. Thats why I propose to add into paragraph heading "comparing controls" and replace unpractical code sample with usable code sample for check input of new password. --- forms/cs/validation.texy | 12 ++++++------ forms/en/validation.texy | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/forms/cs/validation.texy b/forms/cs/validation.texy index 919afc032c..b8ef19407e 100644 --- a/forms/cs/validation.texy +++ b/forms/cs/validation.texy @@ -155,15 +155,15 @@ $form->addText(/* ... */) V Nette lze velmi snadno reagovat na splnění či nesplnění podmínky i na straně JavaScriptu pomocí metody `toggle()`, viz [#dynamický JavaScript]. -Reference mezi prvky -==================== +Reference na jiný prvek +======================= -Jako argument pravidla či podmínky lze uvádět referenci na jiný prvek. Takto lze např. dynamicky validovat, že prvek `text` má tolik znaků, jako je hodnota prvku `length`: +Jako argument pravidla či podmínky lze předat i jiný prvek formuláře. Pravidlo potom použije hodnotu vloženou později uživatelem v prohlížeči. Takto lze např. dynamicky validovat, že prvek `password` obsahuje stejný řetězec jako prvek `password_confirm`: ```php -$form->addInteger('length'); -$form->addText('text') - ->addRule($form::Length, null, $form['length']); +$form->addPassword('password', 'Heslo'); +$form->addPassword('password_confirm', 'Potvrďte heslo') + ->addRule($form::Equal, 'Zadaná hesla se neshodují', $form['password']); ``` diff --git a/forms/en/validation.texy b/forms/en/validation.texy index edc865c357..215f7c21e8 100644 --- a/forms/en/validation.texy +++ b/forms/en/validation.texy @@ -155,15 +155,15 @@ $form->addText(/* ... */) In Nette, it is very easy to react to the fulfillment or not of a condition on the JavaScript side using the `toggle()` method, see [#Dynamic JavaScript]. -References Between Controls -=========================== +Reference to Another Element +============================ -The rule or condition argument can be a reference to another element. For example, you can dynamically validate that the `text` has as many characters as the value of the `length` field is: +As an argument for a rule or condition, you can also pass another form element. The rule will then use the value entered later by the user in the browser. This can be used, for example, to dynamically validate that the `password` element contains the same string as the `password_confirm` element: ```php -$form->addInteger('length'); -$form->addText('text') - ->addRule($form::Length, null, $form['length']); +$form->addPassword('password', 'Password'); +$form->addPassword('password_confirm', 'Confirm Password') + ->addRule($form::Equal, 'The passwords do not match', $form['password']); ```