Problem with "beforeValidate"

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:




<?php

$myprice = intval(substr($model->price, 0, -1)*100); ?>

<h3><?= Html::encode($myprice) ?></h3>



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!

Try




public function beforeValidate()

{

    if (parent::beforeValidate()) {

        $this->price = intval(substr($this->price, 0, -1) * 100);

        return true;

    }

}



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…

Paste your controller and view.

this is a part of the controller




<?php


namespace backend\controllers;


use Yii;

use common\models\Product;

use common\models\search\ProductSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;


class ProductController extends Controller

{

	public function behaviors()

	{

		return [

			'verbs' => [

				'class' => VerbFilter::className(),

				'actions' => [

					'delete' => ['post'],

				],

			],

		];

	}


	/** ACTIONS...**/

	  

	

  

	public function actionUpdate($id)

	{

		$model = $this->findModel($id);


		if ($model->load(Yii::$app->request->post()) && $model->save()) {

			return $this->redirect(['view', 'id' => $model->uid]);

		} else {

			return $this->render('update', [

				'model' => $model,

			]);

		}

	}



And this is a part of the view for the update form




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;


?>

<?php

$test = intval(substr($model->price_level1, 0, -1)*100); ?>

<h3><?= Html::encode($test) ?></h3> 


<div class="row">

    <div class="col-lg-5">


        <?php $form = ActiveForm::begin(); ?>

    

        <?= $form->field($model, 'price')->textInput() ?>

         <h3><?= Html::encode($model->price) ?></h3>

              

        <div class="form-group">

            <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

        </div>


        <?php ActiveForm::end(); ?>

    </div>

</div>



Thanks again!!

Are you allowing price input like XX.YY$ ? If $ is optional you should check if it’s here before removing it.

Anyway, what does $model->getErrors() say?

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:


['attribute' => 'price', 'value' =>  $model->price/100 . '$'],

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.