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

Mandatory Validation When Saving Model???

I've come into another roadblock while developing my app. I'm having an issue when in my model area. I want to for instance save a user, but I don't have all the fields in the users table yet. If for instance I have a users table with id, email, password, phone_number, but in my controller I have the following:

$user = new User();
$user->email = '[email protected]';
$user->password = 'somepassword';
$user->save();

$user->save(); always returns false for me. When I run through php $user->getMessages(); it says "[field] is required" for all the fields I haven't put it. I've tried putting in my model php public function validation() { } but that didn't work. I added the validation code from some of the example pages and I got the same result. I've removed it and gotten the same result. I would like some validation on the fields I am trying to input, but I don't want validation if the field is not put in through $user->something or $user->save($_POST, array('filter_fields'));

TIA

You can try the skipAttributes() in your model's initialize().

class User extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->skipAttributes(
            [
                'field1',
                'field2',
                'field3',
                'field4',
            ]
        );
    }
}
edited Oct '14

If for instance if I have a quick user reg and a full user reg, how would I differentiate those? Do I have to run through an if/switch statement or is there some temporary validation I could use for just the controller/action I need?

// insert code to somehow get controller/action
if($controller == 'user' && $action == 'quick') {
    $this->skipAttributes(...);
} else {
    // don't skip anything
}

It'd be super nice if there were a function that is "I only want to validate these 3 fields". That way if down the road you insert more fields in the table you don't have to explicitly outline each new field it needs to skip.