Enum Validation

With CakePHP 5.1 we can now use improved validation for enums in our apps. enumOnly() and enumExcept() are now available.

Let's use the `UserStatus` backed enum (int values and string labels) to test it. It can be found in source code for details.

$this->getValidator()->add('status', 'validEnum', [
    'rule' => ['enumOnly', [UserStatus::Active, UserStatus::Inactive]],
    'message' => 'Invalid enum value.'
]);

Submit a form


We left the "Deleted" status in there on purpose. Usually it would not be here.

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.


Send your feedback or bugreport!