Skip to content

Commit

Permalink
Check and showcase new enumOnly() validation. (#76)
Browse files Browse the repository at this point in the history
* Check and showcase new enumOnly() validation.

* Check and showcase new enumOnly() validation.

* Update annotations.

* Update enum_validation.php

* update lock file.

* update lock file.

* Adjust constraint
  • Loading branch information
dereuromark authored Sep 15, 2024
1 parent e52abe9 commit d1f8354
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 57 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.2",
"spryker/cakephp-statemachine": "dev-master",
"cakephp/plugin-installer": "^2.0.1",
"cakephp/cakephp": "^5.0.3",
"cakephp/cakephp": "^5.1.0",
"cakephp/bake": "^3.0.2",
"mobiledetect/mobiledetectlib": "4.*",
"dereuromark/cakephp-comments": "dev-master",
Expand Down
114 changes: 58 additions & 56 deletions composer.lock

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

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

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

$user = $table->newEmptyEntity();

if ($this->request->is(['post', 'put'])) {
$user = $table->patchEntity($user, $this->request->getData());
$value = $this->request->getData('status');
$label = UserStatus::tryFrom((int)$value)?->label();
if (!$user->getErrors()) {
$this->Flash->success('Value posted: `' . $value . '` (`' . $label . '`)');
} else {
$this->Flash->error('Value posted: `' . $value . '` (`' . $label . '`)');
}
}

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

/**
* @return void
*/
Expand Down
Loading

0 comments on commit d1f8354

Please sign in to comment.