-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from peckadesign/nette-optional
Nette optional rules
- Loading branch information
Showing
9 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
## Měkká validace pomocí dostupných Nette validátorů | ||
|
||
Nette má velkou sadu validačních pravidel pokrývající obecné validace vstupů. V případě, že byste chtěli některý z nich použít, museli byste si ho volat na frontendu, ale backend o této validaci vůbec neví. Když k formuláři přijde backend programátor, tak bude koukat, kde se ta validace bere. Navíc byste si museli dopsat frontend obsluhu pro konkrétní prvek a volat si Nette validátor. Celé to jde hezky zobecnit a vzájemně propojit. `pd/forms` poskytuje jednoduchý mechanismus jak toho docílit - obecné pravidlo `\Pd\Forms\Rules::NETTE_RULE_PROXY`. | ||
|
||
Obecné pravidlo `\Pd\Forms\Rules::NETTE_RULE_PROXY` navážeme na požadovaný formulářový prvek - to zajistí zpracování (vlastně nezpracování na backendu) a předá potřebná nastavení pro frontendové zpracování. Pravidlo nakonfigurujeme pomocí `Pd\Form\RuleOptions`, které vytvoříme z dostupné továrny metodou `createNetteOptional()`. Ta má dva parametry - Nette pravidlo a argument(y) pro toto pravidlo. | ||
|
||
```php | ||
<?php declare(strict_types = 1); | ||
|
||
class FormFactory | ||
{ | ||
/** | ||
* @var \Pd\Forms\RuleOptionsFactory | ||
*/ | ||
private $ruleOptionsFactory; | ||
|
||
public function __construct(\Pd\Forms\RuleOptionsFactory $ruleOptionsFactory) | ||
{ | ||
$this->ruleOptionsFactory = $ruleOptionsFactory; | ||
} | ||
|
||
public function create(): \Nette\Forms\Form | ||
{ | ||
$form = new \Nette\Forms\Form; | ||
|
||
$companyIdentifierOptions = $this->ruleOptionsFactory->createNetteOptional( | ||
\Nette\Forms\Form::PATTERN, | ||
'([0-9]\s*){0,8}' | ||
); | ||
|
||
$form->addText('companyIdentifier', 'IČO') | ||
->addCondition(\Nette\Forms\Form::FILLED) | ||
->addRule(\Pd\Forms\Rules::NETTE_RULE_PROXY, '_msg_ico_pattern', $companyIdentifierOptions) | ||
; | ||
} | ||
} | ||
``` | ||
|
||
Na frontendu obsluha pd/forms zavolá validátor `pattern` z `netteForms.js` a provede validaci formulářového prvku a výsledek oznámí uživateli. Při odeslání formuláře nedochází k backendové validaci, protože o tu nemáme zájem. Chceme pouze uživatele informovat, že je IČO není validaní, ale necháme ho např. dokončit objednávku, abychom nepřišli o konverzi. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "pd-forms", | ||
"title": "pdForms", | ||
"description": "Customization of netteForms for use in PeckaDesign.", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"author": "PeckaDesign, s.r.o <[email protected]>", | ||
"contributors": [ | ||
"Radek Šerý <[email protected]>", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* @author Radek Šerý <[email protected]> | ||
* @author Vít Kutný <[email protected]> | ||
* | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
* | ||
* - adds custom validation rules for optional rule (non-blocking errors, form can be still submitted) | ||
* - changes some netteForms methods | ||
|
@@ -31,7 +31,7 @@ var pdForms = pdForms || {}; | |
/** | ||
* Version | ||
*/ | ||
pdForms.version = '2.0.1'; | ||
pdForms.version = '2.0.2'; | ||
|
||
|
||
/** | ||
|
@@ -139,15 +139,21 @@ pdForms.validateInput = function(e, $inputs) { | |
|
||
$validate.each(function() { | ||
if ($(this).data('ever-focused')) { | ||
// validate control using nette-rules && pd-rules (which are inside nette-rules actually) | ||
var ret = Nette.validateControl(this); | ||
// assumes the input is valid, therefore removing all messages except those associated with ajax rules; this | ||
// prevents flashing of message, when ajax rule is evaluated - ajax rules removes their messages when the ajax | ||
// rule is evaluated | ||
pdForms.removeMessages(this, false); | ||
|
||
var rules = Nette.parseJSON(this.getAttribute('data-nette-rules')); | ||
rules = pdForms.normalizeRules(rules); | ||
|
||
// validate control using nette-rules && pd-rules (which are inside nette-rules actually) | ||
var valid = Nette.validateControl(this, rules); | ||
|
||
var hasAjaxRule = pdForms.hasAjaxRule(rules); | ||
|
||
// has to be here and not inside validateControl as it should add ok class only if whole input is valid (not only parts of condional rule etc.) | ||
if (ret && ! hasAjaxRule) { | ||
if (valid && ! hasAjaxRule) { | ||
// add pdforms-valid class name if the input is valid | ||
pdForms.addMessage(this, null, pdForms.constants.OK_MESSAGE); | ||
} | ||
|
@@ -160,13 +166,6 @@ pdForms.validateInput = function(e, $inputs) { | |
* Validates form element using optional nette-rules. | ||
*/ | ||
pdForms.validateControl = function(elem, rules, onlyCheck) { | ||
// assumes the input is valid, therefore removing all messages except those associated with ajax rules; this | ||
// prevents flashing of message, when ajax rule is evaluated - ajax rules removes their messages when the ajax | ||
// rule is evaluated; when onlyCheck is true, we dont' want to modify DOM at all | ||
if (! onlyCheck) { | ||
pdForms.removeMessages(elem, false); | ||
} | ||
|
||
// validate rules one-by-one to know which passed | ||
for (var id = 0, len = rules.length; id < len; id++) { | ||
var rule = rules[id]; | ||
|
@@ -521,6 +520,12 @@ Nette.validators.PdFormsRules_phone = function(elem, arg, val) { | |
return Nette.validators.regexp(elem, String(/^\+[0-9]{3} ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/), val); | ||
}; | ||
|
||
Nette.validators.PdFormsRules_netteRuleProxy = function(elem, arg, val) { | ||
var validator = pdForms.formatOperation(arg.context.netteRule); | ||
|
||
return validator in Nette.validators ? Nette.validators[validator](elem, arg.context.netteRuleArgs, val) : true; | ||
}; | ||
|
||
Nette.validators.PdFormsRules_ajax = function(elem, arg, val, value, callback) { | ||
if (typeof callback === 'undefined') { | ||
callback = 'PdFormsRules_ajax'; | ||
|
@@ -593,20 +598,22 @@ Nette.addEvent = function(element, on, callback) { | |
* | ||
*/ | ||
Nette.validateControl = function(elem, rules, onlyCheck) { | ||
|
||
if (!elem.nodeName) { // RadioNodeList | ||
elem = elem[0]; | ||
} | ||
rules = rules || Nette.parseJSON(elem.getAttribute('data-nette-rules')); | ||
|
||
if (! rules) { | ||
rules = Nette.parseJSON(elem.getAttribute('data-nette-rules')); | ||
|
||
// convert arg property in rules into Nette format | ||
rules = pdForms.normalizeRules(rules); | ||
} | ||
|
||
// no rules -> skip element validation | ||
if (rules.length === 0) { | ||
return true; | ||
} | ||
|
||
// convert arg property in rules into Nette format | ||
rules = pdForms.normalizeRules(rules); | ||
|
||
return pdForms.validateControl(elem, rules, onlyCheck); | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters