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

A proplem!! All fields need to set up?(create or save)

<?php
                    $user = new User();
                    $user->email = $postData['email'];
                    $user->password = md5($postData['password']);
                    $user->yy = $postData['yy'];
                    $user->mobil = $postData['mobil'];
                    if ($user->save() == false) {
                        foreach ($user->getMessages() as $message) {
                            echo $message . '<br>';
                        }
                    } else {
                        echo 'ok';
                    }

When I insert a record. I will get message:

name is required

nickname is required

idcard is required

alipay is required

vip is required

ww is required

qq is required

addtime is required

updatetime is required

status is required

But these fields are not need!!! Please help me ...

edited Dec '14

Which version u use?

if 1.3 try:

\Phalcon\Mvc\Model::setup(array(    
    'notNullValidations' => false
));

on app init



15.6k
Accepted
answer

Thank U!!! U are right.

I see these page

https://github.com/phalcon/cphalcon/issues/440

https://forum.phalcon.io/discussion/283/created-at-and-validation

And understand how to solve the proplem.

Paste my code

<?php
// use Phalcon\Db\RawValue;
// in model
    public function beforeValidationOnCreate()
    {
        $notNullAttributes = $this->getModelsMetaData()->getNotNullAttributes($this);
        foreach ($notNullAttributes as $field) {
            if (!isset($this->$field)) {
                $this->$field = new RawValue('default');
            }
        }
    }
edited Dec '14

it is not enough: u need something like this:

    public function beforeValidationOnCreate()
    {
        $notNullAttributes = $this->getModelsMetaData()->getNotNullAttributes($this);
        foreach ($notNullAttributes as $field) {
            if (is_null($this->$field)) {
                $this->$field = new RawValue('default');
            } elseif($this->$field=="") {
                $this->$field = new RawValue("''");
            }
        }
    }

    public function beforeValidationOnUpdate()
    {
        $notNullAttributes = $this->getModelsMetaData()->getNotNullAttributes($this);
        foreach ($notNullAttributes as $field) {
            if(!is_null($this->$field) && $this->$field=="") {
                $this->$field = new RawValue("''");
            }
        }
    }

Or you can simply skip the Attributes on create

Put this in your model:

    public function initialize()
    {
        $this->skipAttributesOnCreate(array('field1', 'field2'));
    }

For on Update use this:

    public function initialize()
    {
        $this->skipAttributesOnUpdate(array('field1', 'field2'));
    }

Bad advice

Or you can simply skip the Attributes on create

Put this in your model:

  public function initialize()
  {
      $this->skipAttributesOnCreate(array('field1', 'field2'));
  }

For on Update use this:

  public function initialize()
  {
      $this->skipAttributesOnUpdate(array('field1', 'field2'));
  }

Bad advice

Or you can simply skip the Attributes on create

Put this in your model:

 public function initialize()
 {
     $this->skipAttributesOnCreate(array('field1', 'field2'));
 }

For on Update use this:

 public function initialize()
 {
     $this->skipAttributesOnUpdate(array('field1', 'field2'));
 }

Why?



15.6k

Very good!! 3Q. And I have another question about the model's validation.

How to differentiate between create and update the validation?

it is not enough: u need something like this:

   public function beforeValidationOnCreate()
   {
       $notNullAttributes = $this->getModelsMetaData()->getNotNullAttributes($this);
       foreach ($notNullAttributes as $field) {
           if (is_null($this->$field)) {
               $this->$field = new RawValue('default');
           } elseif($this->$field=="") {
              $this->$field = new RawValue("''");
          }
       }
   }

   public function beforeValidationOnUpdate()
   {
       $notNullAttributes = $this->getModelsMetaData()->getNotNullAttributes($this);
       foreach ($notNullAttributes as $field) {
           if(!is_null($this->$field) && $this->$field=="") {
              $this->$field = new RawValue("''");
          }
       }
   }


15.6k

Your answer is right too.

But I think Vladimir Khramov's answer is better. Because these functions(like beforeValidationOnCreate<Update>) can writen in BaseModel. But skipAttribute need setup in every model.

My english is poor. Hope U understand!

Bad advice

Or you can simply skip the Attributes on create

Put this in your model:

public function initialize()
{
    $this->skipAttributesOnCreate(array('field1', 'field2'));
}

For on Update use this:

public function initialize()
{
    $this->skipAttributesOnUpdate(array('field1', 'field2'));
}

Why?