Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check and showcase new enumOnly() validation. #76

Merged
merged 10 commits into from
Sep 15, 2024
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
Loading