Question

I'm using the Phalcon framework for PHP, and I would like to save my form into the database, all is working how is supposed to, but I can't save empty strings in some (not required) fields.

How can I save not required field? Every time I save not required fields without input, I get an error like "lastname field is required.".

Anyone?

Sorry, my English is not good, I'm learning...


My controller code is:

$this->view->disable();
$usuario = new Usuarios();

    $usuario->usuario = $this->request->getPost("usuario");
    $usuario->email = $this->request->getPost("email");
    $usuario->senha = $this->request->getPost("senha");
    $usuario->nome = $this->request->getPost("nome");
    $usuario->sobrenome = $this->request->getPost("sobrenome");
    $usuario->icone = $this->request->getPost("icone");
    $usuario->fixo = $this->request->getPost("fixo");
    $usuario->celular = $this->request->getPost("celular");
    $usuario->tipo = $this->request->getPost("tipo");

    $usuario->save();

And I have basic validations in my model...

Was it helpful?

Solution

You can choose one of the these solution which suggested on How to make models use defaults in Phalcon PHP Framework?

Last and simple solution:

$usuario->usuario = $this->request->getPost("usuario") || "";

OTHER TIPS

Late answer, but maybe it will be useful for someone. If you want phalcon to ignore specific fields you need to use skipAttributes():

Example:

class Usuarios extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
    $this->skipAttributes(array('field1', 'field2'));
    }
}

Here is a link to the documentation: http://docs.phalconphp.com/en/latest/reference/models.html#skipping-columns

If you wand to save an empty string for some fields (one of reasons of such an error like "lastname field is required"), you can use Phalcon Model allowEmptyStringValues method:

class Robots extends Model
{
    public function initialize()
    {
        $this->allowEmptyStringValues(['usuario', 'someelse', 'someother']);
    }
}

for allowing empty strings saving for manually listed fields.

Also check your Usuarios model (it have field named "lastname") and your database structure NULL / NOT NULL field attributes.

It might be failing because you're trying to save NULL instead of an empty string. I unfortunately can't reply as a comment because of the reputation requirement, but could you post the contents of your Usuarios model? I need to see what your validations look like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top