S
S
Sergey Beloventsev2017-03-09 18:16:26
Yii
Sergey Beloventsev, 2017-03-09 18:16:26

How to properly validate?

there is a model Productswith its own rulesmodel inherited from it Cartssoobstvenno here is its code
How to do validation?

<?php

namespace frontend\models;

use common\models\Products;
use common\models\Price;
class Carts extends Products
{
    public $price;
    public function rules()
    {
        return [
            [['price'], 'required'],
            [['price'], 'integer'],
        ];
    }
    public function getPrise()
    {
        return $this->hasMany(Price::className(), ['products_id' => 'id']);

    }
}

after that in the controller I get it in this way
$recomended     =   Carts::find()->where('recomended=1')->with('images','price')->all();

and in the view as follows, I pass it to the Cart widget
<?php foreach($recomended as $rec){ Cart::widget(['model'=>$rec])} ?>

here is the widget itself
<?php
namespace frontend\widgets\cart;

use Yii;
use yii\base\Widget;
use common\models\Product;
use common\models\Price;
use frontend\models\CartPosition;
use yz\shoppingcart\ShoppingCart;
use yii\web\Controller;
class Cart extends Widget
{
    public $model;
    public $idCountry;
    public function init()
    {
        parent::init();

    }

    public function run()
    {
        $product=$this->model;
        $idCountry=$this->idCountry;
        /*$price = array_filter($product->price, function($item)use($idCountry) {
            return $item->countryid == $idCountry;
        });*/
        $post=Yii::$app->request->post();
        if(Yii::$app->request->isPjax){
            $position = Product::findOne($post['productId']);
            $prise= Price::find()->where("price=".$post['Product']['price'])->andWhere("product_id=".$post['productId'])->one();
            $prises=$prise->price;
            $volume=$prise->volume;
            $country=$prise->countryid;
            $cartPosition = new CartPosition($position->id, $prises, $country, $volume);
            if ($position) {
                \Yii::$app->cart->put($cartPosition, 1);
                if (Yii::$app->request->isPjax) {
                    \Yii::$app->controller->renderPartial('_success');
                    /*return $this->render('html',[
                        'model'=>$product,
                        'price'=>$price
                    ]);*/
                }
            }
        }else{
            return $this->render('html',[
                'model'=>$product,
                //'price'=>$price,
                'var'=>$idCountry
            ]);
        }
    }
}

and his view
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\Pjax;

Pjax::begin();
$form = ActiveForm::begin(['options' => ['class'=>'customsearch','data-pjax' => true]]);
echo Html::activedropDownList( $model,'price', ArrayHelper::map($model->prise, 'price','volumeName'),['prompt' => 
<?php  ActiveForm::end();?>

and now, when the field is not selected, pricethere is actually a script that converts it selectinto a series of links with a final text input, but the import has an attribute name='Carts[pice]', so it's probably better to say that if the valuetext input is empty, the validation does not work, please explain what I'm doing wrong.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2017-03-09
@slo_nik

Good evening.
Use scripts

M
Maxim Timofeev, 2017-03-10
@webinar

I didn’t really go into it, but apparently what you need is when
. Here is an example a little out of context:

public function rules()
    {
        return [
            [['price'], 'required', 'when'=>function($model){
              return ($model->price === null)?false:true;
            }],
            [['price'], 'integer'],
        ];
    }

In fact, depending on whether the connection returned something or not, we make the 'price' attribute mandatory or not

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question