<commit_before><?php

namespace hipanel\actions;

use hiqdev\hiart\Collection;
use Yii;
use yii\base\Action;

class AddToCartAction extends Action
{
    public $productClass;

    public $bulkLoad = false;

    public function run()
    {
        $data = null;

        $collection = new Collection([
            'model' => new $this->productClass
        ]);
        $cart = Yii::$app->cart;
        $request = Yii::$app->request;
//        $data = $request->isPost ? $request->post() : $request->get();

        if (!$this->bulkLoad) {
            $data = [Yii::$app->request->post() ?: Yii::$app->request->get()];
        }

        if ($collection->load($data)) {
            foreach ($collection->models as $position) {
                if (!$cart->hasPosition($position->getId())) {
                    $cart->put($position);
                    Yii::$app->session->addFlash('success', Yii::t('app', 'Item is added to cart'));
                } else {
                    Yii::$app->session->addFlash('warning', Yii::t('app', 'Item already exists in the cart'));
                }
            }
        }

        if ($request->isAjax) {
            Yii::$app->end();
        } else {
            return $this->controller->redirect(Yii::$app->request->referrer);
        }
    }
}<commit_msg>Add validation before Add To Cart<commit_after><?php

namespace hipanel\actions;

use hiqdev\hiart\Collection;
use Yii;
use yii\base\Action;

class AddToCartAction extends Action
{
    public $productClass;

    public $bulkLoad = false;

    public function run()
    {
        $data = null;

        $collection = new Collection([
            'model' => new $this->productClass
        ]);
        $cart = Yii::$app->cart;
        $request = Yii::$app->request;
//        $data = $request->isPost ? $request->post() : $request->get();

        if (!$this->bulkLoad) {
            $data = [Yii::$app->request->post() ?: Yii::$app->request->get()];
        }

        if ($collection->load($data) && $collection->validate()) {
            foreach ($collection->models as $position) {
                if (!$cart->hasPosition($position->getId())) {
                    $cart->put($position);
                    Yii::$app->session->addFlash('success', Yii::t('app', 'Item is added to cart'));
                } else {
                    Yii::$app->session->addFlash('warning', Yii::t('app', 'Item already exists in the cart'));
                }
            }
        } else {
            Yii::$app->session->addFlash('warning', Yii::t('app', 'Item does not exists'));
        }

        if ($request->isAjax) {
            Yii::$app->end();
        } else {
            return $this->controller->redirect(Yii::$app->request->referrer);
        }
    }
}
{'fraction_non_alphanumeric': 0.17781818181818182, 'fraction_numerical': 0.0, 'mean_word_length': 1.939102564102564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6251828', 'n_tokens_mistral': 809, 'n_tokens_neox': 807, 'n_words': 159}