filter action dude

hi, I want to filter the parameters sent to a particular action, using the validators used in the validation of models, but without using models. To separate this logic to check certain parameters in actions that do not use models. I think that create a ActionFilter make use of validators, but not if I’m reinventing the wheel or there is already a better way.

please someone could give me some advice?

example:

test Controller:




<?php

class TestController extend \yii\web\Controller{


    public function actionFoo($p1, $p2)

    {

        if (!somoValidation($p1) or !someValidation($p2)) {

            throw new \yii\web\BadRequestHttpException("error");

        }

        //action code

    }

}

to>




<?php

class TestController extend \yii\web\Controller{


 	public function behaviors()

    {

        return [

            'validate' => [

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

                'actions' => [

                    "foo" => [

                    //somting like model validation

                    ]

                ],

            ]

        ];

    }

    public function actionFoo($p1, $p2)

    {

        //action code

    }

}

I do not know why should you do it.

Anyway, you can find out the logic of how each validator is working going through the code and use that logic of each validator as separate static functions in a static class eg: apps\models\Myvalidator.php etc.

Then you can use these static functions to validate without using any models.

my intention is filter the action params without mix the code for parameters validation and the action business logic. you have any advice on how I could do it?

It may be interesting to you to read this conversation happened in the last month.

http://www.yiiframework.com/doc-2.0/yii-base-dynamicmodel.html

thank u very much