Beforesave Custom Validation For Unique Records

I’m using the Jstree extension. Validations ‘required, email or url’ are working great, not the same thing for the unique validation (the system seems to bypass it), even if i try to use a custom function to perform it.

Now, I’m trying to validate it “manually” through beforeSave method, but I’ve a problem.

If i use this code, the error doesn’t stop the save process:




        public function beforeSave() {

            $codice = $this->codice;

            $res = $this->findByAttributes(array('codice' => $codice));

            if(isset($res)) {            

                $this->addError('codice', 'Questo codice è già utilizzato.');

            }            

            return parent::beforeSave();

        }



If i add “return false”, the save process stops correctly but the error message doesn’t appear:




        public function beforeSave() {

            $codice = $this->codice;

            $res = $this->findByAttributes(array('codice' => $codice));

            if(isset($res)) {            

                $this->addError('codice', 'Questo codice è già utilizzato.');

                return false;

            }            

            return parent::beforeSave();

        }



What can i do?

Thanks!

beforeSave is not a good place to keep validation in.

Create custom validator as described in docs, if you’re really need to.

I wonder why default ‘unique’ validation does not work for you.

I would like to create a custom validation, but it doesn’t works!!

Do you have used jstree extensions? Do you know if there is some problem with unique validator for ajax validation? All the rest is working great, but it seems to be only a "client" validation.

The problem seems to be that Jstree extensions completely bypass the ajax validation.