cakephp3を使いWebアプリケーションを作っています。
新規でアカウント登録を行う(signupページより入力されたデータをusersテーブルにInsert)ときのvalidationで項目のチェックをしていますが、Modelで書いているUsersTable.phpのvalidationのmessageが表示されません。
ちなみにデータ登録の動きは問題ありませんので、単純にエラーメッセージが表示されずに、エラーが起きた場合には全て**Unable to add user.**と表示されます。
アドバイスをどうぞ宜しくお願いします。
■環境
MAMPで構築
php: 5.5.10
Database: Mysql
■ファイル
View: signup.php
Controller: Userscontroller.php
Model: (Table)UsersTable.php / (Entity)User.php
※(Entity)User.phpではパスワードをハッシュ化
signup.php
<?= $this->Form->create($user) ?> <fieldset> <?php echo $this->Form->text( 'name', array( 'maxlength' => '10', 'type' => 'text', 'placeholder'=>'Name', 'required' =>'false')); echo $this->Form->text( 'email', array( 'maxlength' => '255', 'type' => 'email', 'placeholder'=>'Email Address', 'required' =>'false')); echo $this->Form->text( 'password', array( 'maxlength' => '20', 'type' => 'password', 'placeholder'=>'Password', 'required' =>'false')); echo $this->Form->text( 'confirm_password', array( 'maxlength' => '20', 'type' => 'password', 'placeholder'=>'Confirm Password', 'required' =>'false')); ?> </fieldset> <?= $this->Form->button( 'Register', array('name'=>'register', 'class'=>'btn btn-info btn-block')) ?> <?= $this->Form->end() ?>Userscontroller.php
<?php namespace App\Controller; use App\Controller\AppController; use Cake\Event\Event; class UsersController extends AppController { public function signup() { $this->viewBuilder()->layout('default'); $user = $this->Users->newEntity(); if ($this->request->is('post')) { $user = $this->Users->patchEntity($user, $this->request->data); if ($this->Users->save($user)) { $this->Flash->success(__('The user has been saved. Please check your email, then activate your account.')); return $this->redirect(['action' => 'registered']); } $this->Flash->error(__('Unable to add user.')); } $this->set('user', $user); } } **(Table)UsersTable.php** <?php namespace App\Model\Table; use Cake\ORM\Table; use Cake\Validation\Validator; class UsersTable extends Table { public function validationDefault(Validator $validator) { $validator ->requirePresence('name') ->notEmpty('name', 'An Name is required') ->add('name', [ 'maxLen' => [ 'rule' => ['maxLength', 10], 'message' => 'Name is less than 10 charactors.', ] ]) ->requirePresence('email') ->add('email', 'validFormat',['rule' => 'email'] ) ->notEmpty('email', 'A email is required') ->requirePresence('password') ->add('password', [ 'comWith' => [ 'rule' => ['compareWith', 'confirm_password'], 'message' => 'The password is incorrect and for confirm_password.', ], 'minLen' => [ 'rule' => ['minLength', 8], 'message' => 'At least eight characters.', ] ]) ->notEmpty('password', 'A password is required') ->requirePresence('confirm_password') ->add('confirm_password', [ // Whether same or for confirmation_password // 'minLen' => [ // 'rule' => ['minLength', 8], // 'message' => 'At least eight characters.', // ] ]) ->notEmpty('confirm_password', 'A confirm_password is required'); return $validator; } }回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/08 10:10