我的cakePHP模型中inList的反义词是什么?

问题描述 投票:0回答:2

inList声明您的数组应包含某个字符串值。我正在寻找一个规则,它将在我的模型中排除名称的某些值。

以下代码声明该名称应为Bob,Bobbie或Bobzilla:

'name'  => array(
    'rule' => array('inList', array('Bob', 'Bobbie', 'Bobzilla')),
    'message' => 'Stop it Bob!'
),

我需要用户无法输入任何这些名称。对我来说,好像inList应该是notInList。我尝试了很多方法,但没有一个能把我带到罗马。

如果你能帮助我,我将非常感激!

php cakephp model cakephp-2.0
2个回答
0
投票

看看PHP in_array函数之类的东西

if (!in_array(ENTERED_NAME,YOUR ARRAY)) {

}

0
投票

这是我能够提出的最佳解决方案:

public function itsBob($check) {  
    $bobArr = ['Bob', 'Bobbie', 'Bobzilla'];

    if (!in_array($check['name'], bobArr) {
        return false;
    }
    return true;
}

在$ validation中使用以下行:

'name' => array(
    'rule' => array('itsBob'),
    'message' => 'Stop it bob!'
),

itsBob字面意思与inList相反。

© www.soinside.com 2019 - 2024. All rights reserved.