Answer the question
In order to leave comments, you need to log in
How to properly validate?
there is a model Products
with its own rules
model inherited from it Carts
soobstvenno 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']);
}
}
$recomended = Carts::find()->where('recomended=1')->with('images','price')->all();
<?php foreach($recomended as $rec){ Cart::widget(['model'=>$rec])} ?>
<?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
]);
}
}
}
<?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();?>
price
there is actually a script that converts it select
into 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 value
text 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
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'],
];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question