I’ve been trying to solve this without success. I can’t validate a price field on update. I have a message that the field must be an integer. In the database, the price is saved as an integer (6495 for 64.95$) so I have an afterFind method to show the price corectly:
I have this in my model:
public function afterFind()
{
$this->price = $this->price/100 . "$";
}
public function beforeValidate()
{
$this->price = intval(substr($this->price, 0, -1)*100);
The price is then shown correctly in my views (ie 64.95$)but the beforeValidate method doesn’t seem to work. I tested this in a view and it worked:
The result of this code is that myprice is correctly printed as 6495. It is the same code as the one inserted in the beforeValidate method so where am I wrong?
BTW, the rule for "price" in my model is set to "integer"
Thanks Bizley. I tried exactly like you wrote, and with some variations too (with return parent::beforeValidate(); instead of "return true", etc. Unfortunately, nothing works…
Thanks a lot Bizley. I got it working but I finally opted for another solution. The best approach for me was to simply put the price in the different widgets (listview, gridview, etc) by using this line:
That way, there is no change in the database, except when I update the price via a form, which is than viewed as an integer, as I need it in my database.