CakePHP2.6でウェブアプリ開発をおこなっております。
フォームから送信した値にバリデーションをする際に配列のバリデーションが効きませんでした。
配列のバリデーションを行うには独自関数を用意しないと行けないのでしょうか?
モデル
php
1class SampleModel extends AppModel { 2 $validate = array( 3 'sample' => array( 4 array( 5 'rule' => 'notEmpty', 6 'message' => '料金を入力してください' 7 ), 8 array( 9 'rule' => 'numeric', 10 'message' => '料金は半角数字で入力して下さい' 11 ) 12 ), 13 ); 14}
コントローラー
php
1class SamplesController extends AppController { 2 public function sample_form() { 3 if ($this->request->is('post')) { 4 $this->Sample->set($this->request->data); 5 6 if($this->Sample->validates()){ 7 // フォームの値をセッションに保存 8 $this->Session->write('Sample', $this->request->data); 9 10 // 確認画面にリダイレクト 11 $this->redirect(array('action'=> 'confirm')); 12 } else { 13 // バリデーションエラーメッセージを渡す 14 $this->Session->setFlash( '入力エラーがあります'); 15 } 16 } 17 } 18}
ビュー
php
1<?php 2echo $this->Form->create('Sample'); 3echo $this->Form->input('Sample.0.price', array('type' => 'text', 'after' => '円')); 4echo $this->Form->input('Sample.1.price', array('type' => 'text', 'after' => '円')); 5echo $this->Form->input('Sample.2.price', array('type' => 'text', 'after' => '円')); 6echo $this->Form->end(array('label' => '確認画面へ', 'class' => 'btn btn-primary'));
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。