We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Disable validation

Hi :)

On my project, I'm only using Phalcon's ORM/Model. Validations, MVC and all the rest are responsible by another tools.

I have a table with name, email and password. When I try to update a row, like below, the method is "automagically" validating the fields:

$row = new Models\Admins();
$row->setId( 1 );
$row->setName( 'Test' );
$row->save();

pr( $row->getMessages() );

Returns:

Array ( [0] => Phalcon\Mvc\Model\Message Object ( [_type:protected] => Email [_message:protected] => Value of field 'email' must have a valid e-mail format [_field:protected] => email [_model:protected] => )

)

I'd like to disable this kind of validation on all of my tables. How could I do that?

Thanks again! :)

edited May '15

Thien, very sorry for the long delay, but this doesn't help me, because I want to disable all the field validations.

Is there a way to do this?

Thanks again! =)

Edit:

Now I did this:

abstract class Abstrato extends \Phalcon\Mvc\Model {

    public function initialize() {
        $this->useDynamicUpdate( true );
        pr( $this->getModelsMetaData()->getAttributes( $this ) );//print_r - just to see the columns
        $this->skipAttributes( $this->getModelsMetaData()->getAttributes( $this ) );
    }
    //....

And the same way to insert a row:

$row = new Models\Admins();
$row->setId( 1 );
$row->setName( 'Test' );
$row->save();

pr( $row->getMessages() );

And this is returned:

Array
(
    [0] => id
    [1] => name
    [2] => email
    [3] => password
    [4] => created
    [5] => modified
)
Array
(
    [0] => Phalcon\Mvc\Model\Message Object
        (
            [_type:protected] => Email
            [_message:protected] => Value of field 'email' must have a valid e-mail format
            [_field:protected] => email
            [_model:protected] => 
        )

)

But why the message is still there?

This chunk of code is how I'm using validation in my User model for passwords:


class User extends Model {

    public function initialize() {
        $this->useDynamicUpdate(TRUE);
    }

    public function beforeValidationOnCreate() {
        $this->validate(new SafePassword(['field' => 'password']));

        return $this->validationHasFailed() !== TRUE;
    }

    public function beforeValidationOnUpdate() {
        if ($this->hasChanged('password')) {
            $this->validate(new SafePassword(['field' => 'password']));
        }

        return $this->validationHasFailed() !== TRUE;
    }

}

By logic your database data should pass your model validation, but in the case that the data is formatted after store it, as the case of a password validate it out of validation method.

edited May '15

Thanks for the reply, Óscar!

I "solved" inserting this in Base Model (Abstrato):

public function initialize() {
    $this->useDynamicUpdate( true );
}

public function validationHasFailed() {
    return false;
}

public function validate() {
    return true;
}

I think this is the worst way to disable validations, but at least it's working now.

I'd appreciate if anyone had any more hints.

Thanks again. =)