【问题标题】:validation part not working in cake php验证部分在 cake php 中不起作用
【发布时间】:2012-03-20 22:11:09
【问题描述】:

这是操作网址

  http://localhost/carsdirectory/users/create_ad

create_ad.ctp(在这个文件中我有选择字段,在这个字段中我从 state_name 字段表名状态中获取数据)

 <label class="ls-details-label">State</label>
 <div class="ls-details-box">
 <?php
 foreach ($states as $state)
 {
    $state_new[$state['State']['id']]=$state['State']['state_name'];
 }
    echo $this->Form->input('Location.state_id',array('label'=>false,'options'=>$state_new,'empty'=>' Select ','class'=>'styledselect_form_1'));
 ?>
 </div>

state.php(模型)

<?php
 class State extends AppModel
     {
var $name = 'State';


            var $validate = array(
            'state_name' => array(
            'rule' => 'notEmpty',
            'message' => 'Please select a state')

            );                   
    }
   ?>

users_controller.php(控制器)

 public function create_ad()
 {

 $this->loadModel('Location');
 if($this->Location->saveAll($this->data['Location']))
 {
   $this->data['Car']['location_id'] = $this->Location->id;
 }
 else
  {
$this->Session->setFlash('Detail could not be saved in Cars Controller');
  }
}

但验证不起作用,此消息未显示请选择一个状态,如果我没有选择值,则插入 null

我无法解决这个问题,所以请帮助我

在此先感谢,vikas tyagi

【问题讨论】:

  • 你必须在user模型中编写验证代码!!
  • Rikesh Shah,我应该在用户模型中验证代码
  • 是的,使用state_id在用户模型中编写代码,即用户表中状态的字段名称。
  • 状态表中没有 state_id 字段
  • 应该!!我的意思是您如何将用户表与状态相关联?

标签: php cakephp cakephp-1.3


【解决方案1】:

您似乎正在尝试验证 State.state_name 字段,但您的表单输入是 Location.state_id。确保它们匹配,然后重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2014-01-27
    相关资源
    最近更新 更多