# Conditional rules validation and compare values

**URL:** <https://discourse.cakephp.org/t/conditional-rules-validation-and-compare-values/5890>\
**Category:** Uncategorized\
**Created:** [April 3, 2019, 3:42pm UTC](https://discourse.cakephp.org/t/conditional-rules-validation-and-compare-values/5890 "2019-04-03T15:42:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [April 3, 2019, 3:42pm UTC](https://discourse.cakephp.org/t/conditional-rules-validation-and-compare-values/5890/1 "2019-04-03T15:42:14Z")

</div>

I have users register and provide Role below

```auto
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,

```auto
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

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

```

I hope someone help me to fix error, thanx

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 10, 2019, 4:33pm UTC](https://discourse.cakephp.org/t/conditional-rules-validation-and-compare-values/5890/2 "2019-04-10T16:33:33Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [April 12, 2019, 4:47am UTC](https://discourse.cakephp.org/t/conditional-rules-validation-and-compare-values/5890/3 "2019-04-12T04:47:11Z")

</div>

here structure UsersTable in mysql

```auto
----------------------------------------------------------------------------------------------------------------
| 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

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

```

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 12, 2019, 2:23pm UTC](https://discourse.cakephp.org/t/conditional-rules-validation-and-compare-values/5890/4 "2019-04-12T14:23:49Z")

</div>

`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;
}
```
