我只是在 cakephp(2.x) 中应用验证规则。当我保存数据时,它会在不验证表单的情况下保存到数据库中。我无法找到我缺少的地方。我是 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' ) ),
1 回答
临摹微笑
TA贡献1982条经验 获得超2个赞
如果您分配自己的名称,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');
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报
0/150
提交
取消