我的代码中有一个问题validatin字段usign cakephp3.4.13
它始终显示“此字段不能为空”事件用户名字段具有值输入,但是当我删除userstable.php中的验证时,它将提交,但数据库中的用户名字段为空。
请帮帮我
Userstable.php
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator->requirePresence('username', 'Please enter username.');
$validator
->requirePresence('password', 'create')
->notEmpty('password', 'You must enter a password', 'create')
->add('password', [
'length' => [
'rule' => ['minLength', 8],
'message' => 'Passwords must be at least 8 characters long.',
]
]);
$validator
->requirePresence('age', 'create')
->notEmpty('age');
$validator
->requirePresence('address', 'create')
->notEmpty('address');
$validator
->email('email')
->requirePresence('email', 'create')
->notEmpty('email');
$validator
->requirePresence('gender', 'create')
->notEmpty('gender');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
$rules->add($rules->isUnique(['username']));
return $rules;
}
Userscontroller.php //register
<?php
public function register()
{
$user= $this->Users->newEntity();
if ($this->request->is('post')) {
$image_name = $this->request->data['profile_pic']['name'];
$image_tmp = $this->request->data['profile_pic']['tmp_name'];
$destination = WWW_ROOT.'img'.DS.'users'.DS.$image_name;
move_uploaded_file($image_tmp,$destination);
$this->request->data['profile_pic'] = $image_name;
$this->request->data['destination'] = $destination;
$user= $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('Data has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
?>
User.php //模型/实体
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
/**
* User Entity.
*/
class User extends Entity
{
protected $_accessible = [
'*' => true,
'id' => false
];
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
}
?>
register.ctp
<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
<ul class="side-nav">
<li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?></li>
</ul>
</div>
<div class="users form large-10 medium-9 columns">
<div id="output"></div>
<?php
if ($this->request->session()->read('Auth.User.username')) {
echo"You must logout first ";
print $this->request->session()->read('Auth.User.username');
echo $this->Html->link('Logout',array('controller' =>'users','action' => 'logout'));
}else{
?>
<?php
//$this->Form->create($user, array('class' => 'insertForm'));
echo $this->Form->create($user, array('type'=>'file'));
?>
<fieldset>
<legend><?= __('Add User') ?></legend>
<?php
$date_posted = date("Y-m-d") ;
echo $this->Form->input('username');
echo $this->Form->input('email');
echo $this->Form->input('age');
$options = ['Male' => 'Male', 'Female' => 'Female'];
echo $this->Form->input('gender', array(
'options' => $options,
'type' => 'select',
//'empty' => 'Select the gender',
'label' => 'Gender')
);
echo $this->Form->input('address');
echo $this->Form->input('created',array('type'=>'hidden','value' => $date_posted));
echo $this->Form->input('password');
echo $this->Form->input('profile_pic', ['type' => 'file']);
?>
</fieldset>
<?php echo $this->Form->button('Register', ['type' => 'submit']); ?>
<?php } ?>
</div>