2017-10-08 134 views
1

Phalcon save函數詢問必填字段,即使其可用於post。以前是使用tag,一切工作正常,能夠完成CRUD功能。現在我想實現驗證並將代碼從tag更新爲form;更改後我無法執行saveupdate。爲查看我使用.volt語法來渲染formPhalcon保存無法執行

「名稱是必需的」始終得到錯誤信息即使其 硬編碼。

什麼可能會出錯?

型號:

<?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); 
     } 
    } 
    ?> 

控制器:

<?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']); 
    } 
    ?> 

形式:

<?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); 
     } 
    } 
    ?> 
+0

一種解決方法是去到你的數據庫表中,並設置列設置爲空,而不是NOT NULL:您可以通過設置柱式空數據庫或禁用notNullValidations。 – Ultimater

+0

你也可以檢查'$ this-> request-> getPost()'包含數據。 – Ultimater

+0

@Ultimater'post'正在返回值。另外我不想在數據庫中允許空值。 –

回答

0

這不是在驗證和保存之前發生的無效驗證。

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

有問題?我不希望允許NULL值到數據庫。這是唯一的方法嗎? –

+0

另外我沒有試圖存儲NULL值,當我得到錯誤 –

+0

如果你沒有設置任何值的屬性 - 那麼它是空的默認值。 – Juri