I'm trying to modify a custom validation rule in Yii2. I want to compare three attributes, the scenario is like:
harga_total must be greater than (harga_satuan times banyak_satuan)
This is what I'm doing:
public function rules() {
return [
[['harga_total'], 'validateHarga', 'skipOnEmpty' => false, 'skipOnError' => false],
];
}
public function validateHarga() {
$a = $this->harga_total;
$b = $this->harga_satuan;
$c = $this->banyak_satuan;
if ($a <= ($b * $c)) {
$this->addError('harga_total', 'Harga Total harus lebih besar atau sama dengan harga satuan dikali dengan banyak satuan');
}
}
But the form doesn't show error or anything.
The other validation rules (not in a function like this one) I do in this model work fine.
I also tried doing it in rules function, like this:
['harga_total', 'compare', 'compareAttribute' => ($this->harga_satuan * $this->banyak_satuan), 'operator' => '>=','message' => 'Harga total harus lebih atau sama dengan harga satuan', 'type' => 'number'],
Doesn't work either.
ANSWER:
Turns out the function works but not directly on client side. It works after all other validation rules passed.