Yii Framework Forum: Beforesave Custom Validation For Unique Records - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Beforesave Custom Validation For Unique Records Rate Topic: -----

#1 User is offline   Denis Seghetti 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 05-January 13

Posted 23 November 2013 - 12:51 PM

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!
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 23 November 2013 - 01:24 PM

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.
God is real unless declared as integer
0

#3 User is offline   Denis Seghetti 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 05-January 13

Posted 23 November 2013 - 02:43 PM

View PostORey, on 23 November 2013 - 01:24 PM, said:

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.
0

#4 User is offline   Denis Seghetti 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 05-January 13

Posted 26 November 2013 - 03:40 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users