プログラミングはド素人のものですので、質問の意味などわからないことがあるかもしれませんが
お許しください。
今、CakePHPを使って練習用にブログサイトを作っています。
my_layout.ctpにPostsController.ctpのindexアクションとaddアクションを使用していて、
簡単にいうと、記事の一覧と記事の作成が同じ画面でできるようにしたいのです。
Validator無しではうまくFormヘルパーから入力された値が記事一覧に表示されるのですが、Validatorを組み込んだ際に、Formに打ち込み、Addボタンを押すと、add.ctpを作れというエラーが出てきてしまいます。
Validatorの使用に各アクションのViewを要求するシステムなのがあるのでしょうか?
またエラーを出さない方法、ヒントなどいただければ幸いです。
ここまで読んでいただいた方、ありがとうございました。
<?php namespace App\Controller; class PostsController extends AppController { public function index() { $this->viewBuilder()->layout('my_layout'); $posts = $this->Posts->find('all'); // $this->set('posts', $posts); $this->set(compact('posts')); } public function view($id = null) { $post = $this->Posts->get($id); $this->set(compact('post')); } public function add() { $post = $this->Posts->newEntity(); if ($this->request->is('post')) { $post = $this->Posts->patchEntity($post, $this->request->data); if ($this->Posts->save($post)) { return $this->redirect(['action'=>'index']); } else { } } $this->set(compact('post')); } } ```ここに言語を入力 <?php namespace App\Model\Table; use Cake\ORM\Table; use Cake\Validation\Validator; class PostsTable extends Table { public function initialize(array $config) { $this->addBehavior('Timestamp'); } public function validationDefault(Validator $validator) { $validator ->notEmpty('title') ->requirePresence('title') ->notEmpty('body') ->requirePresence('body') ->add('body', [ 'length' => [ 'rule' => ['minLength', 10], 'message' => 'body length must be 10+' ] ]); return $validator; } } ```ここに言語を入力 my_layout.ctp(Template/Layout) ``` <!DOCTYPE html> <html lang="ja"> <head> <?= $this->Html->charset() ?>PostsController.php
</head> <body> <h1>Blog Posts</h1><title> <?= $this->fetch('title') ?> </title> <?= $this->Html->css('styles.css') ?>
<ul> <?php foreach ($posts as $post) : ?> <li><?= $this->Html->link($post->title,['action'=>'view',$post->id]); ?></li> <?php endforeach; ?> </ul> <?= $this->Form->create(null, [
'url' => ['action'=>'add']
]);?>
<?= $this->Form->input('title');?>
<?= $this->Form->input('body',['maxlength'=>'3']);?>
<?= $this->Form->button('Add');?>
<?= $this->Form->end();?>
</body> </html><section class="container"> <?= $this->fetch('content') ?> </section>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/09/17 06:40