# Custom validation rule in model

**URL:** <https://discourse.cakephp.org/t/custom-validation-rule-in-model/4781>\
**Category:** Uncategorized\
**Created:** [August 21, 2018, 8:46pm UTC](https://discourse.cakephp.org/t/custom-validation-rule-in-model/4781 "2018-08-21T20:46:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ole](https://avatars.discourse-cdn.com/v4/letter/o/fbc32d/32.png) [@ole](https://discourse.cakephp.org/u/ole)\
**Post date:** [August 21, 2018, 8:46pm UTC](https://discourse.cakephp.org/t/custom-validation-rule-in-model/4781/1 "2018-08-21T20:46:11Z")

</div>

Hi,

i read this documentation [https://book.cakephp.org/3.0/en/core-libraries/validation.html](https://book.cakephp.org/3.0/en/core-libraries/validation.html) but i did not get it…

my validation function:  
…

```
$validator
        ->boolean('a')
        ->requirePresence('a', 'create')
        ->notEmpty('a');

    $validator
        ->boolean('b')
        ->requirePresence('b', 'create')
        ->notEmpty('b');

    $validator
        ->scalar('c')
        ->maxLength('c', 255)
        ->allowEmpty('c');

    return $validator;

```

Possibilities:  
a = 1 and b = 0  
a = 0 and b = 1  
a = 0 and b = 0 and c = not empty

Can some one help me?

Thanks!!!

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [August 21, 2018, 8:59pm UTC](https://discourse.cakephp.org/t/custom-validation-rule-in-model/4781/2 "2018-08-21T20:59:13Z")

</div>

Validation is for individual fileds. If you need logic involved more fileds than use application rules.

[https://book.cakephp.org/3.0/en/orm/validation.html#application-rules](https://book.cakephp.org/3.0/en/orm/validation.html#application-rules)

---

<div class="post-metadata">

**Author:** ![ole](https://avatars.discourse-cdn.com/v4/letter/o/fbc32d/32.png) [@ole](https://discourse.cakephp.org/u/ole)\
**Post date:** [August 22, 2018, 6:27pm UTC](https://discourse.cakephp.org/t/custom-validation-rule-in-model/4781/3 "2018-08-22T18:27:48Z")

</div>

could you give a example to me?

I try to check in validation this:

if ($this-\>request-\>getData(‘a’)== 1 and $this-\>request-\>getData(‘b’)== 1){  
return false  
}

would be nice 😉

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [August 22, 2018, 9:05pm UTC](https://discourse.cakephp.org/t/custom-validation-rule-in-model/4781/4 "2018-08-22T21:05:20Z")

</div>

You would need something like this

```
$rules->add(function ($entity, $options) {
    return ($entity->a == 1) && ($entity->b == 1);
}, 'aAndBAreOne');

```

Or if the check is more complicated than use an entity method

[https://book.cakephp.org/3.0/en/orm/validation.html#using-entity-methods-as-rules](https://book.cakephp.org/3.0/en/orm/validation.html#using-entity-methods-as-rules)

---

<div class="post-metadata">

**Author:** ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)\
**Post date:** [August 23, 2018, 2:10pm UTC](https://discourse.cakephp.org/t/custom-validation-rule-in-model/4781/5 "2018-08-23T14:10:38Z")

</div>

You can use validation rules on multiple fields. In fact there is a core one that does that `equalToField()`.

Validation is for verifying that data posted/injected by a user is correct (i.e. checking that entities have valid and expected data), while application rules are for testing application state before something is persisted into the database.

What @ole is describing sounds like validation to me 🙂

You can easily do

```php
$validator->add('a', 'correctValues', [
    'rule' => function ($data, $provider) {
        $a = $data;
        $b = $provider['data']['b'];
        $c = $provider['data']['c'];

        return !($a == 1 && $b == 1);
    }
]);

```

---

<div class="post-metadata">

**Author:** ![ole](https://avatars.discourse-cdn.com/v4/letter/o/fbc32d/32.png) [@ole](https://discourse.cakephp.org/u/ole)\
**Post date:** [August 24, 2018, 9:27am UTC](https://discourse.cakephp.org/t/custom-validation-rule-in-model/4781/6 "2018-08-24T09:27:38Z")

</div>

Thanks! I’ll try it at weekend 😉 !
