前提・実現したいこと
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></body><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>
コントローラー
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
回答3件
あなたの回答
tips
プレビュー