【问题标题】:How can I match the strings in Yii?如何匹配 Yii 中的字符串?
【发布时间】:2012-03-20 15:30:37
【问题描述】:

我被困住了。假设我有 string1 和 string 2(在同一模型中),其中 string1 有 7 位数字,而 string2 有 2 位数字。 然后我想将它们匹配在一起,其中 2 个 string1 的开头数字必须与 string2 相同。我在模型中尝试了这段代码>>

public function CekDigit($attribute,$params)
{

$attribute=substr($this->string1,0,-7);
$params=$this->string2;

if($this->$attribute==$params['subject'])
{
$this->addError(‘Error’, $params['message']);
return false;
}
}

在规则中,我放了这段代码>>

    <?php

array('string1','cekdigit','message'=>'the code is unmatched','subject'),

?>

在我编写完该代码后,我尝试用 23xxxxxxx 填充 $string1 但我收到一个错误,即 CException : Property model.23 is not defined。有人可以帮我吗?非常感谢

【问题讨论】:

  • 你能在 CekDigit() 中打印 $_POST 并复制粘贴结果吗?
  • 我不认为你知道那里发生了什么......请仔细阅读 - yiiframework.com/wiki/168/create-your-own-validation-rule
  • 看起来您在某个阶段将$string1 称为$$string1。此外,看起来您将$params 设置为字符串($params=$this-&gt;string2),但随后您尝试与数组值进行比较($params['subject']

标签: php yii match substr


【解决方案1】:

使用你的例子$attribute=substr($this-&gt;string1,0,-7); 将 $attribute 设置为 23,所以行 if($this-&gt;$attribute==$params['subject']) 将检查$params['subject']$this-&gt;23

试试

public function CekDigit($attribute, $params)
{ 
    $string1 = substr($this->$attribute, 0, 2); 
    if ($string1 == $this->string2)
        {
        $this->addError(‘Error’, $params['message']);
        return false;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多