質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Q&A

解決済

3回答

1678閲覧

cakephp3で未入力のレコードはDBに登録しないようにしたいです。

sintarien

総合スコア16

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

0グッド

0クリップ

投稿2020/06/23 04:47

前提・実現したいこと

cakephp3で未入力の入力フォームはDBに登録しないようにしたいです。
CakePHPで見積書制作ツールを作っています。
現在商品登録ページでfor文で作った10行のアイテム登録フォームがあるのですが、未入力のフォームもそのまま登録されてしまうという状態です。
これを「入力フォームは10行あるけれど、入力されていなければDBに登録しない」という風にしたいです。
どういう処理をかけばよいでしょうか?

このように空欄のレコードも登録されてしまいます
イメージ説明

Entityがレコードに対しての処理が出来るのかな?
と思っているのでEntityに処理を書けばよいのでしょうか?

viewページ

<body> <div class="container-fluid "> <div class="wrapper"> <div class="content-wrapper"> <section class="content"> <form action="" method="post" class="form-horizontal" class="estimate"> <h2 class="form-header">取引先情報</h2> <table class="table"> <caption>見積情報</caption> <thead> <?= $this->Form->create($estimation) ?> <?php $date = date("Ymd-H:i:s");?> <?php echo $this->Form->hidden('number', ['value'=>date("Y/m/d H:i:s")]);?> <?php echo $this->Form->hidden('file_name', ['value'=> $date.".pdf"]);?> <?php echo $this->Form->hidden('created_date', ['value'=>date('Y/m/d')]);?> <tr> <th>納入期日<?php echo $this->Form->control('elivery_date', ['label' => '']);?></th> </tr> <th>受渡場所<?php echo $this->Form->control('elivery_place', ['label' => '']);?></th> </tr> <th>支払条件<?php echo $this->Form->control('payment', ['label' => '']);?></th> </tr> <tr> <th>荷造運賃<?php echo $this->Form->control('packing_fare', ['label' => '']);?></th> </tr> <tr> <th>有効期限<?php echo $this->Form->control('expiration_date', ['label' => '']);?></th> </tr> <tr> <th>仕向地<?php echo $this->Form->control('destination', ['label' => '']);?></th> </tr> <tr> <th>備考<?php echo $this->Form->control('remarks', ['label' => '','style' => 'width:100%']);?></th> </tr> <?php echo $this->Form->hidden('status'); ?> <?php echo $this->Form->hidden('stamp1_user_id', ['options' => $users, 'value' => $user['id']]); ?> <?php echo $this->Form->hidden('stamp2_user_id', ['options' => $users, 'empty' => true]); ?> <?php echo $this->Form->hidden('department_id', ['options' => $departments, 'value' => $user['department']]); ?> <?php //echo $this->Form->hidden('department_id', ['options' => $departments, 'value' => $department_id]);?> <?php echo $this->Form->hidden('client_id', ['options' => $clients, 'value' => $client_id]); ?> <?php echo $this->Form->hidden('deleted',['value' => 0]); ?> </thead> </table>
<table class="table"> <caption>商品入力フォーム</caption> <col width="100" /> <col width="100" /> <col width="100" /> <col width="100" /> <col width="100" /> <col width="100" /> <thead> <tr> <th>品名</th> <th>荷姿</th> <th>容器</th> <th>UN No.</th> <th>単価(/kg)</th> </tr> <?php for ($i=1; $i <= 10; $i++): ?> <tr> <th><?php echo $this->Form->control("items.${i}.item_name", ['label' => '']);?></th> <th><?php echo $this->Form->control("items.${i}.packing", ['label' => '']);?></th> <th><?php echo $this->Form->control("items.${i}.container", ['label' => '']);?></th> <th><?php echo $this->Form->control("items.${i}.un_number", ['label' => '']);?></th> <th><?php echo $this->Form->control("items.${i}.unit_price", ['label' => '']);?></th> <th><?php echo $this->Form->hidden("items.${i}.price");?></th> <th><?php echo $this->Form->hidden("items.${i}.quantity", ['label' => '']);?></th> <th><?php echo $this->Form->hidden("items.${i}.deleted"); ?></th> <th><?php echo $this->Form->hidden("items.${i}.number", ['label' => '']);?></th> </tr> <?php endfor;?> </thead> <tfoot> <tr> <th colspan="5"> <tr> <div class="btn"> <?= $this->Form->button(__('新規登録')) ?> </div> </tr> </th> </tr> </tfoot> <?= $this->Form->end() ?> </table> </form> </section> </div> </div> </div>
</body>

コントローラー

public function add($client_id=null) { $estimation = $this->Estimations->newEntity(); if ($this->request->is('post')) { $estimation = $this->Estimations->patchEntity($estimation, $this->request->getData(), ['associated' => ['Items']]); if ($this->Estimations->save($estimation)) { $this->Flash->success(__('The estimation has been saved.')); return $this->redirect(['action' => 'view',$estimation->id]); } $this->Flash->error(__('The estimation could not be saved. Please, try again.')); } $users = $this->Estimations->Users->find('list', ['limit' => 200]); $departments = $this->Estimations->Departments->find('list', ['limit' => 200]); $clients = $this->Estimations->Clients->find('list', ['limit' => 200]); $this->set(compact('estimation', 'users', 'departments', 'clients')); //est_id: addのviewの中で$est_idとして使う //estimation_id:このメソッド内の$estimation_id $this->set('client_id',$client_id); $this->set('user',$this->Auth->user()); }

補足情報(FW/ツールのバージョンなど)

cakephp3.8

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

CakePHP|バリデーションを行えばいいのでは?

PHP

1 2$validator 3 ->requirePresence('title') 4 ->notEmpty('title', 'このフィールドに入力してください') 5 ->add('title', [ 6 'length' => [ 7 'rule' => ['minLength', 10], 8 'message' => 'タイトルは 10 文字以上必要です', 9 ] 10 ]) 11 ->allowEmpty('published') 12 ->add('published', 'boolean', [ 13 'rule' => 'boolean' 14 ]) 15 ->requirePresence('body') 16 ->add('body', 'length', [ 17 'rule' => ['minLength', 50], 18 'message' => '記事は中身のある本文を持っていなければなりません。', 19 ]); 20

投稿2020/06/23 05:14

kai0310

総合スコア2070

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

バリデーションを掛けるのはもちろんですが、空の入力行をエンティティへのセット時に弾くようにします。

beforeMarshalを使用します。

エンティティー構築前のリクエストデータ変更 - データの保存 - 3.9

EstimationsTable に beforeMarshalメソッドを追加します。

php

1public function beforeMarshal(Event $event, ArrayObject $data) 2{ 3 if (isset($data['items'])) { 4 $data['items'] = array_filter($data['items'], static function ($item) { 5 // 値が空でないかの判定、ちょっと雑なので適宜修正してください 6 return count(array_filter(array_values($item))) > 0; 7 }); 8 } 9}

投稿2020/06/24 04:37

nojimage

総合スコア957

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sintarien

2020/06/24 06:37

無事うまくいきました!! まだbeforeMarshalについて深く理解できていませんが、もっと調べてみます! ありがとうございました!
guest

0

まず、各カラムのテーブル定義をNotNULLにしましょう。
その状態でbakeコマンドでModelファイルを作り直せば(できればtemplateも)、自動的に最低限未入力(フォームで空欄)のものは保存できない作りにはなります。
それ以上の細かいルールについては、ヴァリデーションを定義することで対応しましょう。

投稿2020/06/23 13:55

CodeLab

総合スコア1939

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問