Preg_match error in Post Request

Hello,

I have a listbox in my form, for multiple selection of values. I pass an array variable to it, to get the selection from the end-user.

Below is my form code [view]:





    <?= $form->field($model, 'recipient_roles')->listBox($model->mailRecipientsUserType(), ['multiple' => true, 'class' => 'role-box form-control']) ?>

    

    <?= $form->field($model, 'recipient_cc_roles')->listBox($model->mailRecipientsUserType(), ['multiple' => true, 'class' => 'role-box form-control']) ?>




Below is the model function:




    public function mailRecipientsUserType(){

        $user_type = [];

       $array = ['Apple','Mango','Cherry'];

        foreach($array as $a){

            $user_type[$a] = $a;

        }

        return $user_type;

    }




And I am getting below Error:

preg_match() expects parameter 2 to be string, array given

on this line of controller:




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



Thanks in advance!!

Check your validation rules for "recipient_roles" and "recipient_cc_roles".

The "each" validator should be used for them.

http://www.yiiframework.com/doc-2.0/yii-validators-eachvalidator.html

Thanks Softark for your input. I tried that as well. Still the same error




[['recipient_roles', 'recipient_cc_roles'], 'each', 'rule' => ['string']],

OR

[['recipient_roles', 'recipient_cc_roles'], 'each', 'rule' => ['array']],



That’s strange.

Could you read the detailed call stack?




 PHP Warning – yii\base\ErrorException

preg_match() expects parameter 2 to be string, array given


    1. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\ezyang\htmlpurifier\library\HTMLPurifier\Lexer.php at line 348

    338339340341342343344345346347348349350351352353354355356357


            return $html;

        }

     

        /**

         * Takes a string of HTML (fragment or document) and returns the content

         * @todo Consider making protected

         */

        public function extractBody($html)

        {

            $matches = array();

            $result = preg_match('!<body[^>]*>(.*)</body>!is', $html, $matches);

            if ($result) {

                return $matches[1];

            } else {

                return $html;

            }

        }

    }

     

    // vim: et sw=4 sts=4


    2. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\ezyang\htmlpurifier\library\HTMLPurifier\Lexer.php – HTMLPurifier_Lexer::extractBody() at line 318

    312313314315316317318319320321322323324


            // extract body from document if applicable

            if ($config->get('Core.ConvertDocumentToFragment')) {

                $e = false;

                if ($config->get('Core.CollectErrors')) {

                    $e =& $context->get('ErrorCollector');

                }

                $new_html = $this->extractBody($html);

                if ($e && $new_html != $html) {

                    $e->send(E_WARNING, 'Lexer: Extracted body');

                }

                $html = $new_html;

            }

     


    3. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\ezyang\htmlpurifier\library\HTMLPurifier\Lexer\DOMLex.php – HTMLPurifier_Lexer::normalize() at line 50

    44454647484950515253545556


         * @param HTMLPurifier_Config $config

         * @param HTMLPurifier_Context $context

         * @return HTMLPurifier_Token[]

         */

        public function tokenizeHTML($html, $config, $context)

        {

            $html = $this->normalize($html, $config, $context);

     

            // attempt to armor stray angled brackets that cannot possibly

            // form tags and thus are probably being used as emoticons

            if ($config->get('Core.AggressivelyFixLt')) {

                $char = '[^a-z!\/]';

                $comment = "/<!--(.*?)(-->|\z)/is";


    4. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\ezyang\htmlpurifier\library\HTMLPurifier.php – HTMLPurifier_Lexer_DOMLex::tokenizeHTML() at line 216

    210211212213214215216217218219220221222


                        // list of un-purified tokens

                        $lexer->tokenizeHTML(

                            // un-purified HTML

                            $html,

                            $config,

                            $context

                        ),

                        $config,

                        $context

                    )

                );

     

            for ($i = $filter_size - 1; $i >= 0; $i--) {


    5. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\helpers\BaseHtmlPurifier.php – HTMLPurifier::purify() at line 57

    51525354555657585960616263


     

            if ($config instanceof \Closure) {

                call_user_func($config, $configInstance);

            }

            static::configure($configInstance);

     

            return $purifier->purify($content);

        }

     

        /**

         * Allow the extended HtmlPurifier class to set some default config options.

         * @param \HTMLPurifier_Config $config

         * @since 2.0.3


    6. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\base\Model.php – yii\helpers\BaseHtmlPurifier::process() at line 693

    687688689690691692693694695696697698699


        public function setAttributes($values, $safeOnly = true)

        {

            if (is_array($values)) {

                $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes());

                foreach ($values as $name => $value) {

                    if (isset($attributes[$name])) {

                        $value= \yii\helpers\HtmlPurifier::process($value);

                        $this->$name = $value;

                    } elseif ($safeOnly) {

                        $this->onUnsafeAttribute($name, $value);

                    }

                }

            }


    7. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\base\Model.php – yii\base\Model::setAttributes() at line 801

    795796797798799800801802803804805806807


            $scope = $formName === null ? $this->formName() : $formName;

            if ($scope === '' && !empty($data)) {

                $this->setAttributes($data);

     

                return true;

            } elseif (isset($data[$scope])) {

                $this->setAttributes($data[$scope]);

     

                return true;

            } else {

                return false;

            }

        }


    8. in D:\WAMP\wamp\www\vmap_combined\basic\controllers\TemplateController.php – yii\base\Model::load() at line 59

    53545556575859606162636465


         * @return mixed

         */

        public function actionCreate()

        {

            $model = new EmailTemplate();

     

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

                $templateRole = Yii::$app->request->post('EmailTemplate');

                $roles = isset($templateRole['recipient_roles']) ? $templateRole['recipient_roles'] : [];

                $model->recipient_roles = $model->toCsvString($roles);

                $roles = isset($templateRole['recipient_cc_roles']) ? $templateRole['recipient_cc_roles'] : [];

                $model->recipient_cc_roles = $model->toCsvString($roles);

                $model->created_on = date('Y-m-d');


    9. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\base\InlineAction.php – app\controllers\TemplateController::actionCreate() at line 55

    495051525354555657


            $args = $this->controller->bindActionParams($this, $params);

            Yii::trace('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__);

            if (Yii::$app->requestedParams === null) {

                Yii::$app->requestedParams = $args;

            }

     

            return call_user_func_array([$this->controller, $this->actionMethod], $args);

        }

    }


    10. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\base\InlineAction.php – call_user_func_array:{D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\base\InlineAction.php:55}() at line 55

    495051525354555657


            $args = $this->controller->bindActionParams($this, $params);

            Yii::trace('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__);

            if (Yii::$app->requestedParams === null) {

                Yii::$app->requestedParams = $args;

            }

     

            return call_user_func_array([$this->controller, $this->actionMethod], $args);

        }

    }


    11. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\base\Controller.php – yii\base\InlineAction::runWithParams() at line 151

    145146147148149150151152153154155156157


            }

     

            $result = null;

     

            if ($runAction && $this->beforeAction($action)) {

                // run the action

                $result = $action->runWithParams($params);

     

                $result = $this->afterAction($action, $result);

     

                // call afterAction on modules

                foreach ($modules as $module) {

                    /* @var $module Module */


    12. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\base\Module.php – yii\base\Controller::runAction() at line 455

    449450451452453454455456457458459460461


            $parts = $this->createController($route);

            if (is_array($parts)) {

                /* @var $controller Controller */

                list($controller, $actionID) = $parts;

                $oldController = Yii::$app->controller;

                Yii::$app->controller = $controller;

                $result = $controller->runAction($actionID, $params);

                Yii::$app->controller = $oldController;

     

                return $result;

            } else {

                $id = $this->getUniqueId();

                throw new InvalidRouteException('Unable to resolve the request "' . ($id === '' ? $route : $id . '/' . $route) . '".');


    13. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\web\Application.php – yii\base\Module::runAction() at line 84

    78798081828384858687888990


                $params = $this->catchAll;

                unset($params[0]);

            }

            try {

                Yii::trace("Route requested: '$route'", __METHOD__);

                $this->requestedRoute = $route;

                $result = $this->runAction($route, $params);

                if ($result instanceof Response) {

                    return $result;

                } else {

                    $response = $this->getResponse();

                    if ($result !== null) {

                        $response->data = $result;


    14. in D:\WAMP\wamp\www\vmap_combined\basic\vendor\yiisoft\yii2\base\Application.php – yii\web\Application::handleRequest() at line 375

    369370371372373374375376377378379380381


            try {

     

                $this->state = self::STATE_BEFORE_REQUEST;

                $this->trigger(self::EVENT_BEFORE_REQUEST);

     

                $this->state = self::STATE_HANDLING_REQUEST;

                $response = $this->handleRequest($this->getRequest());

     

                $this->state = self::STATE_AFTER_REQUEST;

                $this->trigger(self::EVENT_AFTER_REQUEST);

     

                $this->state = self::STATE_SENDING_RESPONSE;

                $response->send();


    15. in D:\WAMP\wamp\www\vmap_combined\basic\web\index.php – yii\base\Application::run() at line 12

    6789101112


     

    require(__DIR__ . '/../vendor/autoload.php');

    require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

     

    $config = require(__DIR__ . '/../config/web.php');

     

    (new yii\web\Application($config))->run();







$value= \yii\helpers\HtmlPurifier::process($value);



This line is causing the problem, but it is not there in the original core framework code.

Did you add it? Don’t do it.

Great. Thank you so much. It worked!!

Not sure, when I added this line. Thanks once again…