rulesChecker with OR-condition

Hi,

in my model i have rule

    $rules->add($rules->existsIn(['user_id'], 'Users'));

How can I check if ‘user_id’ exists in ‘Users’ OR in ‘Dummyusers’?

Thank you!

Ole

Assuming Users and Dummyusers are associated with your model, and that $entity->id is what you’re looking for in those two other models, then the following might work for you:

$rules->add( function ( $entity, $options ) {
    $user = $this->Users->find( 'all' )->where( [ 'Users.user_id' => $entity->id ] )->first();
    $dummyuser = $this->Dummyusers->find( 'all' )->where( [ 'Dummyusers.user_id' => $entity->id ] )->first();

    if ( empty( $user ) && empty( $dummyuser ) ) {
        return false;
    } else {
        return true;
    }
}, 'userInUsersOrDummyusers' );

I tried this quickly myself to test and it seems like it should work, or at least point you in the right direction.

https://book.cakephp.org/3.0/en/orm/validation.html#conditional-dynamic-error-messages

Hey Ryan,
Thanks a lot!

It works fine.

Ole