Conditional rules validation and compare values

#1

I have users register and provide Role below

Template\Users\add.ctp

<?php 
echo $this->Form->create($user) ;
//...more field ...
echo $this->Form->radio(
	'Role',
	[
	    ['value' => 'Administrator', 'text' => 'Administrator'],
	    ['value' => 'Penulis Artikel', 'text' => 'Penulis Artikel'],
	    ['value' => 'Pengunjung', 'text' => 'Pengunjung'],
	]);
//...more field
echo $this->Form->submit('TAMBAH');
echo $this->Form->end();
?>

next I have provide conditional rules and both compare,

Model\Table\UsersTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Rule\IsUnique;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class UsersTable extends Table
{
   public function buildRules(RulesChecker $rules)
   {
      $rules->add(function ($entity, $options) use($rules) {
		     if ($entity->Role == 'Administrator') {
			  $rule = $rules->existsIn('User', 'Administrator');
			  return $rule($entity, $options);
		     }
		     return false;
	         }, 'Administrator tidak boleh lebih dari 1');
		 return $rules;
   }
}

the conditional rules inside (UsersTable) that I want output is compare field values Role == Administrator, not greater than one.
the error messages shown is

ExistsIn rule for 'User' is invalid. 'Administrator' is not associated with 'App\Model\Table\UsersTable'.

I hope someone help me to fix error, thanx

0 Likes

#2

The first argument to existsIn is the column name where the value to check is found in the entity. Does your entity have a column called User?

The second argument is the associated table to look up the value in. Does your UsersTable have an association called Administrator?

0 Likes

#3

here structure UsersTable in mysql

----------------------------------------------------------------------------------------------------------------
|  Nama    |      Role              |      Dibuat           |        Diubah        |       Aksi                |  
----------------------------------------------------------------------------------------------------------------
|  Budi    |   Administrator        |  04-04-2019 14:33:29  |                      |   Tampil  Perbarui  Hapus |
---------------------------------------------------------------------------------------------------------------

the rule validation inside UsersTable.php, that’s I want, is prevent add new user with Role = Administrator, more than one,…simply is Role = Administrator just only one users.

I’m tired after reading in

https://book.cakephp.org/3.0/en/orm/query-builder.html

that’s hard to understand.
I home someone can help me,…thanx

0 Likes

#4

existsIn is to make sure that a value exists in another table. For example, if you’re adding a post with a user id, to confirm that the user id is for a real user. Seems that you’re not looking to cross-reference another table here, so existsIn is not at all the right thing. Sounds like you want something more like:

if ($entity->Role == 'Administrator' &&
    $this->find()->where(['Role' => 'Administrator'])->count() != 0)
{
    return false;
}
0 Likes