Custom validation method


Hi all,

I am trying to add validation that simply checks to make sure two fields are different.

I have found a method that does the opposite and makes sure two fields are the same.

Can someone please help me do this?

Do I need to write a custom validation method?

Can you please help me with sample code or point me to the documentation that steps me through how to do this.



In XXXTable.php

public function validationDefault(Validator $validator)
            ->add('field_name1', [
                'NotEqualToField2' => [ //you can define this name
                    'rule' => function ($value, $context) { // this $value refer to ->add('field_name1'
                        //custom logic return true / false
                        return $value !== $context['data']['field_name2'];
                    'message' => 'Fields must be different'
        return $validator;


Diego this is awesome!

I plugged this into my code and it worked perfectly!

Thanks so much.