2017-10-08 5 views
1

Phalcon save機能は、postから利用可能な場合でも必須フィールドを要求しています。以前はtagを使用していましたが、すべてが正常に機能しており、完了するためにはCRUDの機能を実行できました。今私はtagからformへの検証とアップグレードされたコードを実装したいと思っていました。変更後に私はsaveまたはupdateを実行できませんでした。私は.voltの構文を使用してformを表示しています。 「の名前が必要になる」と Phalconセーブできません

は、必ずエラーメッセージが出
場合でも、その ハードコードされました。

何が間違っていたでしょうか?

モデル:

<?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ではなくNULLに設定することができます。 – Ultimater

+0

'$ this-> request-> getPost()'にデータが入っているかどうかをチェックすることもできます。 – Ultimater

+0

@Ultimater 'post'が値を返しています。また、私はDBにnull値を許可したくありません。 –

答えて

0

これは、検証前および保存時に発生するnotNullValidationsです。

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

質問がありますか?データベースにNULL値を許可したくありません。これが唯一の方法ですか? –

+0

また、エラーが発生したときにNULL値を格納しようとしていません。 –

+0

プロパティに値を設定しないと、デフォルトでNULLになります。 – Juri

関連する問題