Phalcon save unable to perform

722 Views Asked by At

Phalcon save function is asking required fields even if its available from post. Previously was using tag and everything was working fine and able to do complete CRUD functionality. Now i wanted to implement validation and upgraded code from tag to form; after changes i couldn't perform save or update. For view i'm using .volt syntax to render form.

Always getting error message as "Name is required" even if its hard-coded.

what would be possibly went wrong?

Model:

<?php
    class Invoicestatus extends \Phalcon\Mvc\Model
    {
        protected $id;
        protected $name;
        protected $description;
        public function setId($id)
        {
            $this->id = $id;

            return $this;
        }
        public function setName($name)
        {
            $this->name = $name;

            return $this;
        }
        public function setDescription($description)
        {
            $this->description = $description;

            return $this;
        }
        public function getId()
        {
            return $this->id;
        }
        public function getName()
        {
            return $this->name;
        }
        public function getDescription()
        {
            return $this->description;
        }
        public function initialize()
        {
            $this->setSchema("invoice");
            $this->setSource("invoicestatus");
            $this->hasMany(
                'Id',
                'Invoice',
                'InvoiceStatusId',
                [
                    'alias' => 'Invoice',
                    'foreignKey' => [
                        'message' => 'The invoice status cannot be deleted because other invoices are using it',
                    ]
                ]
            );
        }
        public function getSource()
        {
            return 'invoicestatus';
        }
        public static function find($parameters = null)
        {
            return parent::find($parameters);
        }
        public static function findFirst($parameters = null)
        {
            return parent::findFirst($parameters);
        }
    }
    ?>

Controller:

<?php
    $form = new InvoicestatusForm();
    $invoicestatus = new Invoicestatus();
    $data = $this->request->getPost();

    if (!$form->isValid($data, $invoicestatus)) {
        $messages = $form->getMessages();

        foreach ($messages as $message) {
            $this->flash->error($message);
        }

        return $this->dispatcher->forward(
            [
                "action"     => "new",
            ]
        );
    }
    //$invoicestatus->name = $this->request->getPost('name', 'string');
    //$invoicestatus->description = $this->request->getPost('description', 'string');
    //$success = $invoicestatus->save();
    $success = $invoicestatus->save($data, array('name', 'description'));
    if($success)
    {
        $form->clear();
        $this->flash->success("Invoice Status successfully saved!");
        $this->dispatcher->forward(['action' => 'index']);
    }
    else
    {
        $this->flash->error("Following Errors occured:");
        foreach($invoicestatus->getMessages() as $message)
        {
            $this->flash->error($message);
        }
        $this->dispatcher->forward(['action' => 'new']);
    }
    ?>

Form:

<?php
    class InvoicestatusForm extends Form
    {
        public function initialize($entity = null, $options = null)
        {
            if (isset($options['edit']) && $options['edit']) {
                $id = new Hidden('id');
            } else {
                $id = new Text('id');
            }
            $this->add($id);
            $name = new Text('name', ['placeholder' => 'Name']);
            $name->setFilters(["striptags","string",]);
            $name->addValidators([new PresenceOf(["message" => "Name is required...",])]);
            $this->add($name);
            $description = new Text('description', ['placeholder' => 'Description']);
            $this->add($description);
        }
    }
    ?>
1

There are 1 best solutions below

5
Juri On

This is notNullValidations which happens before validation and on save. You can either set column type NULL in database or disable notNullValidations by:

Model::setup(
    [
        'notNullValidations' => false,
    ]
);