-2
我正在嘗試在我的cakePHP項目中註冊一個用戶。我完成了登錄&註冊教程,完美的工作。自從我試圖重複使用我的項目中的代碼之後,我卻沒有運氣。登錄部分工作很好,但註冊每次都失敗。任何幫助都會很棒。在CakePHP中爲我的網站註冊一個用戶
在AdminController.php我的註冊功能
public function register(){
$user = $this->Admin->newEntity();
if($this->request->is('post')){
$user = $this->Admin->patchEntity($user, $this->request->data);
if($this->Admin->save($user)){
$this->Flash->success('You are registered');
return $this->redirect(['action' => 'login']);
} else {
$this->Flash->error('You have not registered');
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
我聯繫/ register.ctp
<br>
<div class = "index large-4 medium-4 large-offset-4 medium-offset-4">
<div class = "panel">
<h2 class = "text-centre">Please Register</h2>
<?= $this->form->create($user); ?>
<?= $this ->form->input('first name'); ?>
<?= $this ->form->input('last name'); ?>
<?= $this ->form->input('date of birth', array('type' => 'date')); ?>
<?= $this ->form->input('email'); ?>
<?= $this ->form->input('password', array('type' => 'password')); ?>
<?= $this ->form->submit('register', array('class' => 'button')); ?>
<?= $this->form->end(); ?>
</div>
</div>
我的表,我想在
CREATE TABLE `admin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fName` varchar(255) NOT NULL,
`lName` varchar(255) NOT NULL,
`dOb` date NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
註冊我的用戶這是AdminTable .php
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Admin Model
*
* @method \App\Model\Entity\Admin get($primaryKey, $options = [])
* @method \App\Model\Entity\Admin newEntity($data = null, array $options = [])
* @method \App\Model\Entity\Admin[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\Admin|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Admin patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\Admin[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\Admin findOrCreate($search, callable $callback = null)
*
* @mixin \Cake\ORM\Behavior\TimestampBehavior
*/
class AdminTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('admin');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create')
->add('id', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator
->requirePresence('fName', 'create')
->notEmpty('fName');
$validator
->requirePresence('lName', 'create')
->notEmpty('lName');
$validator
->date('dOb')
->requirePresence('dOb', 'create')
->notEmpty('dOb');
$validator
->email('email')
->requirePresence('email', 'create')
->notEmpty('email');
$validator
->requirePresence('password', 'create')
->notEmpty('password');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
$rules->add($rules->isUnique(['id']));
return $rules;
}
}
在此先感謝您花時間瀏覽一下。
謝謝你的幫助。添加代碼調試後,我得到的錯誤:
[
'fName' => [
'_required' => 'This field is required'
],
'lName' => [
'_required' => 'This field is required'
]
]
你能提供詳細的錯誤信息嗎? – Steve
由於此代碼'$ this-> Flash-> error('您還沒有註冊');'我唯一的錯誤是**您沒有註冊**。無論如何,我可以找出錯誤的原因是什麼? – Peakapot
你可以共享'AdminTable.php'因爲驗證它不會保存數據。 –