-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check and showcase new enumOnly() validation.
- Loading branch information
1 parent
2472d24
commit 26e51f7
Showing
5 changed files
with
97 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
50 changes: 50 additions & 0 deletions
50
plugins/Sandbox/templates/CakeExamples/enum_validation.php
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,50 @@ | ||
<?php | ||
/** | ||
* @var \App\View\AppView $this | ||
* @var \Sandbox\Model\Entity\SandboxUser $user | ||
*/ | ||
|
||
?> | ||
|
||
<?php $this->append('script'); ?> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css"> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script> | ||
<!-- and it's easy to individually load additional languages --> | ||
<script>hljs.highlightAll();</script> | ||
<?php $this->end(); ?> | ||
|
||
<h2>Enum Validation</h2> | ||
<p> | ||
With CakePHP 5.1 we can now use improved validation for enums in our apps. enumOnly() and enumExcept() are now available. | ||
</p> | ||
|
||
<p> | ||
Let's use the `UserStatus` backed enum (int values and string labels) to test it. It can be found in source code for details. | ||
</p> | ||
|
||
<?php | ||
$code = <<<'CODE' | ||
$this->getValidator()->add('status', 'validEnum', [ | ||
'rule' => ['enumOnly', [UserStatus::Active, UserStatus::Inactive]], | ||
'message' => 'Invalid enum value.' | ||
]); | ||
CODE; | ||
|
||
echo $this->Highlighter->highlight($code, ['lang' => 'php']); | ||
?> | ||
|
||
<h4>Submit a form</h4> | ||
<?php echo $this->Form->create($user); ?> | ||
<?php echo $this->Form->control('status'); ?> | ||
<?php echo $this->Form->submit(); ?> | ||
<?php echo $this->Form->end(); ?> | ||
|
||
<br> | ||
|
||
<p> | ||
We left the "Deleted" status in there on purpose. Usually it would not be here. | ||
</p> | ||
<p> | ||
Here you can see that now the "user" can only select "Active" and "Inactive". "Deleted" is not an allowed value for this form, even | ||
though it is in the enum. | ||
</p> |
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