Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



8.8k
Accepted
answer

Thank U!!! U are right.

I see these page

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

http://forum.phalconphp.com/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?



8.8k

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("''");
          }
       }
   }


8.8k

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?