-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Model validation placeholders. Fixes #772
- Loading branch information
1 parent
7502719
commit 75a8a59
Showing
4 changed files
with
108 additions
and
4 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
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 |
---|---|---|
|
@@ -456,6 +456,37 @@ and simply set ``$validationRules`` to the name of the validation rule group you | |
protected $validationRules = 'users'; | ||
} | ||
|
||
Validation Placeholders | ||
----------------------- | ||
|
||
The model provides a simple method to replace parts of your rules based on data that's being passed into it. This | ||
sounds fairly obscure but can be especially handy with the ``is_unique`` validation rule. Placeholders are simply | ||
the name of the field (or array key) that was passed in as $data surrounded by curly brackets. It will be | ||
replaced by the **value** of the matched incoming field. An example should clarify this:: | ||
|
||
protected $validationRules = [ | ||
'email' => 'required|valid_email|is_unique[users.email,id,{id}]' | ||
]; | ||
|
||
In this set of rules, it states that the email address should be unique in the database, except for the row | ||
that has an id matching the placeholder's value. Assuming that the form POST data had the following:: | ||
|
||
$_POST = [ | ||
'id' => 4, | ||
'email' => '[email protected]' | ||
] | ||
|
||
then the ``{id}`` placeholder would be replaced with the number **4**, giving this revised rule:: | ||
|
||
protected $validationRules = [ | ||
'email' => 'required|valid_email|is_unique[users.email,id,4]' | ||
]; | ||
|
||
So it will ignore the row in the database that has ``id=4`` when it verifies the email is unique. | ||
|
||
This can also be used to create more dynamic rules at runtime, as long as you take care that any dynamic | ||
keys passed in don't conflict with your form data. | ||
|
||
Protecting Fields | ||
----------------- | ||
|
||
|