Phalcon model validation

Hello,

I am using the StringLenght validation on a column "name", but in the database it's set as default NULL, but when I use the stringLength function, it requires the field to be filled no matter what. I can't leave it NULL and I dont use PresenceOf?

How can I return NULL, if name is not filled?



53.0k

https://docs.phalconphp.com/en/3.0.1/reference/validation.html#avoid-validate-empty-values

but perhaps you have to

if ($_POST['name'] === "") {
    $name = null;
}

What i am trying to do is:

if($userid){
$comment->userid = $userid;
}else{
$generate = new \Models\Statistics\Statistics();
$comment->guestid = $generate->uniqid();
$comment->name = $this->request->getPost('name');
}

The problem is, that I dont need to past a name to comment->name if userid exist. However, it says it still need to be filled. The default in database is NULL?



53.0k

test when you add "allowEmpty" => true to your stringLength validator

It's can't be empty if i want it to be set.

I only need it to ask stringLenght when I use $robot->name But if i dont use that, then ignore the validation, becuase I dont need to set it

Hope you understand



53.0k

nope sorry, i really dont get it. post few examples if you still need a solution

Just use allowEmpty :C