Cake 3.x Rules and messages

Hi, When I setup a rule in my table class as seen below, the error message never shows up in the view underneath the control?

$rules->add($rules->isUnique([‘email’], ‘This email address is already in use.’));

Try to do this way:

$rules->add($rules->isUnique([‘email’]), ['message‘ => 'This email address is already in use.’]);

See ya.

I still do not see this message show up underneath my control in my view? Do rules not work this way? Sorta like when you have a validation message with a minimum length password

Here is my rule code in my Table Class:

public function buildRules(RulesChecker $rules)
// Add a rule that is applied for create and update operations
// $rules->add(function ($entity, $options) {
// // Return a boolean to indicate pass/failure
// }, ‘ruleName’);

    // // Add a rule for create.
    // $rules->addCreate(function ($entity, $options) {
    //     // Return a boolean to indicate pass/failure
    // }, 'ruleName');

    // // Add a rule for update
    // $rules->addUpdate(function ($entity, $options) {
    //     // Return a boolean to indicate pass/failure
    // }, 'ruleName');

    // // Add a rule for the deleting.
    // $rules->addDelete(function ($entity, $options) {
    //     // Return a boolean to indicate pass/failure
    // }, 'ruleName');

    // A list of fields
    $rules->add($rules->isUnique(['email']), ['message' => 'This email address is already in use']);
    return $rules;

How is your template file looks like? Is your input generated by the from helper?

<? $this->extend(’/Common/view’); ?>

<div class=“users form huge-top”>

<?= $this->Form->create($users, [‘type’ => ‘post’]) ?>


<legend><?= __(‘Edit User’) ?></legend>

<?= $this->Form->control(‘username’) ?>

<?= $this->Form->control(‘first_name’) ?>

<?= $this->Form->control(‘last_name’) ?>

<?= $this->Form->control(‘email’) ?>

<?= $this->Form->hidden(‘password’) ?>

<?= $this->Form->label(‘Role’) ?>

<?= $this->Form->select(‘role’, [‘Admin’, ‘Employee’]); ?>

<?= $this->Form->error(‘email’); ?>


<?= $this->Form->button(__(‘Submit’)); ?>

<?= $this->Form->end() ?>


this seems to be ok

What is in your validationDefault() method?

public function validationDefault(Validator $validator)
->notEmpty(‘password’, ‘You must enter a password’)
//->sameAs(‘password’,‘confirm_password’,‘Passwords do not match.’)
->add(‘password’, [
‘length’ => [
‘rule’ => [‘minLength’, 8], ‘message’ => ‘Passwords must be at least 8 characters long.’],

    return $validator;

Plus the rule validation is weird. If flags and a@b.com2 as the same

I can not see any obvious error in your code. What I would do is to bake the model and check the difference between your validation related code and the generated ones.

What is the best way to inspect why the save is failing? Maybe that may shed some light?

sure, after you try to save $this->YourTable->save($yourEntity) you should see all errors in $yourEntity->errors()

Yep it is flagging the email as an error but the error message does not show

What is the exact output?

array(1) { [“email”]=> array(1) { [“unique”]=> string(20) “The email is in use.” } }

Are you sure the email address is not already in the database?

It is I am testing a case where a user tries to enter an email address that is already in the database but I thought on the form below the email field it would have a message saying The email is in use but that message never shows in the template

Does the messsage not automatically show up in the template or do I need to get the message and show it in the flash message?

Did you checked the html if the error message is there and not just hidden by your own css/js?

oh possible let me check