Skip to content

Commit

Permalink
References Between Controls - more clear description, better code sample
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mildabre authored and dg committed Oct 23, 2024
1 parent 52cdef3 commit 51840cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions forms/cs/validation.texy
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
```


Expand Down
12 changes: 6 additions & 6 deletions forms/en/validation.texy
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
```


Expand Down

0 comments on commit 51840cf

Please sign in to comment.