Skip to content

Commit

Permalink
Check and showcase new enumOnly() validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Mar 17, 2024
1 parent 2472d24 commit 26e51f7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"php": ">=8.1",
"spryker/cakephp-statemachine": "dev-master",
"cakephp/plugin-installer": "^2.0.1",
"cakephp/cakephp": "^5.0.3",
"cakephp/cakephp": "dev-5.x-enum-only-except as 5.0.6",
"cakephp/bake": "3.x-dev as 3.0.2",
"mobiledetect/mobiledetectlib": "4.*",
"dereuromark/cakephp-tinyauth": "dev-master",
Expand Down
19 changes: 13 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions plugins/Sandbox/src/Controller/CakeExamplesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ public function enums() {
$this->set(compact('user'));
}

/**
* @return \Cake\Http\Response|null|void
*/
public function enumValidation() {
/** @var \Sandbox\Model\Table\SandboxUsersTable $table */
$table = $this->fetchTable('Sandbox.SandboxUsers');
$table->getValidator()->add('status', 'validEnum', [
'rule' => ['enumOnly', [UserStatus::Active, UserStatus::Inactive]],
'message' => 'Invalid enum value.',
]);

/** @var \Sandbox\Model\Table\SandboxUsersTable $user */
$user = $table->newEmptyEntity();

if ($this->request->is(['post', 'put'])) {
$user = $table->patchEntity($user, $this->request->getData());

Check failure on line 64 in plugins/Sandbox/src/Controller/CakeExamplesController.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Parameter #1 $entity of method Cake\ORM\Table::patchEntity() expects Cake\Datasource\EntityInterface, Sandbox\Model\Table\SandboxUsersTable given.

Check failure on line 64 in plugins/Sandbox/src/Controller/CakeExamplesController.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Parameter #1 $entity of method Cake\ORM\Table::patchEntity() expects Cake\Datasource\EntityInterface, Sandbox\Model\Table\SandboxUsersTable given.
$value = $this->request->getData('status');
$label = UserStatus::tryFrom((int)$value)->label();

Check failure on line 66 in plugins/Sandbox/src/Controller/CakeExamplesController.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Cannot call method label() on Sandbox\Model\Enum\UserStatus|null.

Check failure on line 66 in plugins/Sandbox/src/Controller/CakeExamplesController.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Cannot call method label() on Sandbox\Model\Enum\UserStatus|null.
if (!$user->getErrors()) {

Check failure on line 67 in plugins/Sandbox/src/Controller/CakeExamplesController.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to an undefined method Sandbox\Model\Table\SandboxUsersTable::getErrors().

Check failure on line 67 in plugins/Sandbox/src/Controller/CakeExamplesController.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to an undefined method Sandbox\Model\Table\SandboxUsersTable::getErrors().
$this->Flash->success('Value posted: `' . $value . '` (`' . $label . '`)');
} else {
$this->Flash->error('Value posted: `' . $value . '` (`' . $label . '`)');
}
}

$this->set(compact('user'));
}

/**
* @return void
*/
Expand Down
50 changes: 50 additions & 0 deletions plugins/Sandbox/templates/CakeExamples/enum_validation.php
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>
5 changes: 5 additions & 0 deletions plugins/Sandbox/templates/CakeExamples/enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@ public function initialize(array \$config): void {
<p>
Here you can see that it is now a backed enum object again.
</p>

<h4>More examples</h4>
<ul>
<li><?php echo $this->Html->link('Validation', ['action' => 'enumValidation']);?></li>
</ul>

0 comments on commit 26e51f7

Please sign in to comment.