79680339

Date: 2025-06-26 10:53:16
Score: 2.5
Natty:
Report link

I am able to get it working. Posting my solution in case anyone face similar issue.

Here is how my model code looks like

    public function rules()
    {
        return
        [
            ['code','validateCaseInsensitive']
        ];
    }

    public function validateCaseInsensitive($attribute, $params)
    {
        $query = self::find()
            ->where('LOWER(code) = LOWER(:code)', [':code' => strtolower($this->$attribute)]);

        // Exclude current record on update
        if (!$this->isNewRecord) {
            $query->andWhere(['<>', 'id', $this->id]);
        }
        
        if ($query->exists()) {
            $this->addError($attribute, 'Code already exists.');
        }
    }
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Me too answer (2.5): face similar issue
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: DevD