PHP code example of yii2-webivan1 / yii2-validate-action-params

1. Go to this page and download the library: Download yii2-webivan1/yii2-validate-action-params library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yii2-webivan1 / yii2-validate-action-params example snippets

 
    use webivan\validateAction\ValidateActionTrait;

    public function behaviors()
    {
        return [
            'validation' => [
                'class' => webivan\validateAction\ValidateActionBehavior::class,
                // 'actions' => ['index', 'about'] // Validate only action
            ]
        ];
    }

    public function actionIndex(int $number, string $name, array $data) 
    {
        // ...
    }

    /**
     * @validate
     * @param integer $number
     * @param string $name
     * @param array $data
     */
    public function actionIndex($number, $name, array $data) 
    {
        // ...
    }
 
    public function actionIndex(Request $request, int $number, string $name, array $data, Response $response) 
    {
        // ...
    }
 
    /**
     * @validate
     * @param \yii\web\Request $request
     * @param integer $number
     * @param string $name
     * @param array $data
     * @param Response $response
     */
    public function actionIndex($request, $number, $name, $data, Response $response) 
    {
        // ...
    }
 
    // Usually
    public function actionIndex($cityId) 
    {
        if (is_null($city = City::findOne(['id' => intval($cityId)]))) {
            throw new HttpExceprion(404);
        }
        
        return $city;
    }
    
    // Now
    public function actionIndex(City $city) 
    {
        return $city;
    }
    
    // Or
    
    /**
     * @validate
     * @param \app\models\City $city
     */
    public function actionIndexNew($city) 
    {
        return $city;
    }
    
    // Or
    
    /**
     * @validate
     * @param City $city
     */
    public function actionIndexNew(City $city) 
    {
        return $city;
    }
 
    public function beforeAction($action)
    {
        if (!parent::beforeAction($action)) {
            throw new HttpException(404);
        }
        
        return true;
    }

    // If user is guest, Error 404
    public function actionIndexNew(User $user) 
    {
        // Error 404
    }
    
    // If user is login, User::findOne(['id' => \Yii::$app->user->id])
    public function actionIndexNew(User $user) 
    {
        return $user;
    }