お初にお目にかかります。ぼっくんと申します。
この度、CakePHP3でシステム構築しようとして躓いてしまったため、
こちらでご相談させていただきます。
前提・実現したいこと
https://book.cakephp.org/3.0/ja/core-libraries/form.html
こちらのページを参考に、入力データをチェックし、
正しく入力されたときのみ別ページへ遷移させようとしています。
発生している問題・エラーメッセージ
ImageForm.phpでValidatorを定義しているはずなのですが、
バリデートされずに次の画面に遷移してしまいます。
エラーメッセージ
ImageController.php
<?php namespace App\Controller; use Cake\Routing\Router; use Cake\Event\Event; // CakePHP EventHandler (beforeFilterを使用するのに必要) use Cake\Http\Middleware\CsrfProtectionMiddleware; // CSRFミドルウェア // Form use App\Form\ImageForm; class ImageController extends AppController { private $data; public function beforeFilter(Event $e) { parent::beforeFilter($e); $this->loadComponent('Config'); $this->Config->Initialize([]); } public function index() { $this->loadComponent('File'); $this->loadComponent('Csrf'); $form = new ImageForm(); if ($this->request->is('post')) { if ($form->execute($this->request->getData())) { $this->Flash->success('すぐにご連絡いたします。'); return $this->redirect(['action' => 'send', $this->request->getData()]); } else { $this->Flash->error('フォーム送信に問題がありました。'); } $this->autoRender = true; } $this->set('form', $form); $this->set('title', 'CakePHP3トップページ'); } // 略 }
ImageForm.php
<?php namespace App\Form; use Cake\Form\Form; use Cake\Form\Schema; use Cake\Validation\Validator; class ImageForm extends Form { protected function _buildSchema(Schema $schema) { $schema->addField('Image', ['type' => 'String', 'length' => 10]) ->addField('image_file', ['name' => 'file', 'type' => 'text']); return $schema; } protected function _buildValidator(Validator $validator) { $validator->notEmpty('Image', __('値が入力されていません')); $validator->add('Image', 'length', ['rule' => ['maxLength', 5], 'message' => '最大文字数を超えています'] ); return $validator; } protected function _execute(array $data) { return true; } }
index.ctp
<?=$this->fetch('css') ?> <?php echo $this->Html->css('HomePage/'. $controller. '/design'); ?> <?=$this->Form->create($form, ['type'=>'post', 'url'=>['controller'=>$controller, 'action'=>'send']]) ?> <?=$this->Form->control($controller, ['required' => 'false']) ?> <?=$this->Form->create('File', ['type' => 'file', 'enctype' => 'multipart/form-data', 'url'=>['controller'=>$controller, 'action'=>'send']]); ?> <?=$this->Form->file('file') ?> --> <div class='footer_char'>※同じ名前のファイルは複数保存されず、上書きされます</div> <?=$this->Form->submit('画像を登録する') ?> <?=$this->Form->end() ?>
試したこと
<?=$this->Form->create($form, ['type'=>'post', 'url'=>['controller'=>$controller, 'action'=>'send']]) ?> <?=$this->Form->control($controller, ['required' => 'false']) ?> <?=$this->Form->create('File', ['type' => 'file', 'enctype' => 'multipart/form-data', 'url'=>['controller'=>$controller, 'action'=>'send']]); ?> <?=$this->Form->file('file') ?> -->
index.ctpの上記部分を下記のように修正してみました。
<?=$this->Form->create($form) ?> <?=$this->Form->control($controller, ['required' => 'false']) ?> <?=$this->Form->control($file) ?>
そうすると、バリデートは動くようになったのですが、
バリデートが一致しても次のページん遷移しなくなってしまいました……。
蒸気を受けて、Controllerでむりやりredirectしようとしましたが
遷移前のPostデータが渡せなくなったりと八方ふさがりの状態です…。
CakePHP3どころか、CakePHPやMVCフレームワーク自体苦手だったりするので
もしかしたらごく基本的なところで躓いているのかもしれません……。
ご教授いただけると幸いです。
補足情報(FW/ツールのバージョンなど)
PHP7.1
CakePHP3.6
あなたの回答
tips
プレビュー