【问题标题】:CakePHP validate fields that aren't in the DatabaseCakePHP 验证不在数据库中的字段
【发布时间】:2013-08-07 13:45:10
【问题描述】:

我有一个表格,其中的值不在我的数据库中。我不想对它们进行验证,所以我使用字段名称制定了一些验证规则。不知何故,验证规则没有被使用。

我已经尝试过设置不同的规则。文件名正确,因为 $hasMany 连接有效。

希望你能帮上忙!

表格:

<?php
  echo $this->Form->create('Map');
    echo $this->Form->input('min_x');
    echo $this->Form->input('max_x');
    echo $this->Form->input('min_y');
    echo $this->Form->input('max_y');
  echo $this->Form->end('Submit');
?>

验证规则:

public $validate = array(
  'min_x' => array(
    'rule' => 'Numeric',
    'message' => 'Please enter a numeric value.'
  ),
  'max_x' => array(
    'rule' => 'Numeric',
    'message' => 'Please enter a numeric value.'
  ),
  'min_y' => array(
    'rule' => 'Numeric',
    'message' => 'Please enter a numeric value.'
  ),
  'max_y' => array(
    'rule' => 'Numeric',
    'message' => 'Please enter a numeric value.'
  ),
);

【问题讨论】:

  • $validate 在什么类和文件名?
  • @ಠ_ಠ 好问题,应该在Map 模型中。
  • 如前所述,它位于正确的文件中。它在模型文件夹的 Map.php 文件中。类是:class Map extends AppModel
  • 其实,没关系:规则是否区分大小写? 'rule' =&gt; 'Numeric''rule' =&gt; 'numeric'
  • 它们区分大小写。

标签: php validation cakephp


【解决方案1】:

验证规则区分大小写,因此:

'rule' => 'Numeric',

应该改为

'rule' => 'numeric',

每个实例。

最后一个字段的数组还有一个额外的逗号。


public $validate = array(
  'min_x' => array(
    'numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter a numeric value.'
    )
  ),
  'max_x' => array(
    'numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter a numeric value.'
    )
  ),
  'min_y' => array(
    'numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter a numeric value.'
    )
  ),
  'max_y' => array(
    'numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter a numeric value.'
    )
  )
);

【讨论】:

  • 您确定在您拥有工作的$hasMany 和现在之间没有其他任何变化吗?听起来 $validate 被忽略了
  • 我确定我刚刚做了一个查询并且使用了 $hasMany。
  • 再一次尝试:尝试我编辑中的代码。我在末尾删除了一个额外的逗号并重新格式化了规则
  • @Ryflex 我想得越多,我就越确定有一个被抑制的语法错误导致了这个问题。额外的逗号可能会阻止返回实际的验证结果。
  • 我删除了多余的逗号,愚蠢的我忽略了它,但不幸的是这不是问题。也尝试了您的版本,结果相同。
【解决方案2】:

在所有消息的末尾必须有','。

'max_x' => array(
    'numeric' => array(
        'rule' => 'numeric',
        'message' => 'Please enter a numeric value.',
    )
  ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2017-05-26
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多