Press "Enter" to skip to content

Yii2 – Validation Case Sensitive on PostgreSQL

0

Last updated on 18 Feb 2018

Yii2 – Validation Case Sensitive on PostgreSQL

First

Insert code below in your Model

 

public function getUsernameToLower() {
    return strtolower($this->username);
}

 

and insert code below in rules function

[['nama_marketplace'], 'unique', 'targetAttribute' => ['usernameToLower' => 'lower(username)']],

 

You can also using filter, e.g:

[['logo_marketplace'], 'file', 'skipOnEmpty' => true, 
    'extensions' => 'png, jpg', 
     'message' => Yii::t('app', 'Logo Marketplace tidak boleh kosong.')],

['nama_marketplace', 'unique', 'skipOnError' => true, 
    'targetClass' => MarketplaceUsers::className(), 
    'targetAttribute' => ['nama_marketplace' => 'lower(nama_marketplace)'], 
    'filter' => function($query) {
         $query->where[1] = [
            "lower(nama_marketplace)" => strtolower($this->nama_marketplace)
         ];
     }
]