我只是在 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 關注
- 107 瀏覽
添加回答
舉報
0/150
提交
取消