cakephp 验证不适用于 save()
cakephp validation doesn't work on save()
我只是在 cakephp(2.x) 中应用验证规则。当我保存数据时,它会在不验证的情况下保存到数据库中 form.I 我无法找到我缺少的地方。我是 cakephp 的初学者。请建议我如何验证我的表格。我的表单结构如下所示。
<form class="form-horizontal" method="POST" action="saveUser">
<fieldset>
<legend>Register</legend>
<span class = "error">* Required field</span>
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name <span class="error">*</span></label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="name" placeholder="Name" required value="">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Email <span class="error">*</span></label>
<div class="col-lg-10">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" required value="">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-lg-2 control-label">Password <span class="error">*</span></label>
<div class="col-lg-10">
<input type="password" class="form-control" id="inputPassword" name="password" placeholder="Password" required value="">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
然后模型像这样验证数组
public $validate = array(
'name' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Only Allows Letter and Numbers'
),
'between' => array(
'rule' => array('lengthBetween', 5, 15),
'message' => 'length upto 5 to 15 character'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Password Too Short'
),
'email' => 'email',
);
和像
这样的控制器
if($this->request->is('post')){
if($this->User->save($this->request->data)){
$this->Session->setFlash(__('Employee Registration Successfully'));
$this->redirect(['action'=>'viewAdminDashboard']);
}
else{
$this->Session->setFlash(__('Cannot Register Employee'));
}
}
如果您分配自己的名称,CakePHP 无法将输入字段与模型列匹配。例如:
<input type="text" class="form-control" id="inputName" name="name" placeholder="Name" required value="">
^^^^
...应该是
name="data[User][name]"
如果您将 CakePHP 用于模型和控制器,那么将它也用于视图会更容易:
<?php
echo $this->Form->create('User');
echo $this->Form->input('name');
我强烈推荐Blog Tutorial。
我只是在 cakephp(2.x) 中应用验证规则。当我保存数据时,它会在不验证的情况下保存到数据库中 form.I 我无法找到我缺少的地方。我是 cakephp 的初学者。请建议我如何验证我的表格。我的表单结构如下所示。
<form class="form-horizontal" method="POST" action="saveUser">
<fieldset>
<legend>Register</legend>
<span class = "error">* Required field</span>
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name <span class="error">*</span></label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="name" placeholder="Name" required value="">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Email <span class="error">*</span></label>
<div class="col-lg-10">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" required value="">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-lg-2 control-label">Password <span class="error">*</span></label>
<div class="col-lg-10">
<input type="password" class="form-control" id="inputPassword" name="password" placeholder="Password" required value="">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
然后模型像这样验证数组
public $validate = array(
'name' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Only Allows Letter and Numbers'
),
'between' => array(
'rule' => array('lengthBetween', 5, 15),
'message' => 'length upto 5 to 15 character'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Password Too Short'
),
'email' => 'email',
);
和像
这样的控制器if($this->request->is('post')){
if($this->User->save($this->request->data)){
$this->Session->setFlash(__('Employee Registration Successfully'));
$this->redirect(['action'=>'viewAdminDashboard']);
}
else{
$this->Session->setFlash(__('Cannot Register Employee'));
}
}
如果您分配自己的名称,CakePHP 无法将输入字段与模型列匹配。例如:
<input type="text" class="form-control" id="inputName" name="name" placeholder="Name" required value="">
^^^^
...应该是
name="data[User][name]"
如果您将 CakePHP 用于模型和控制器,那么将它也用于视图会更容易:
<?php
echo $this->Form->create('User');
echo $this->Form->input('name');
我强烈推荐Blog Tutorial。