OctoberCMS plugin show all the validations at once

后端 未结 3 1030
迷失自我
迷失自我 2021-01-24 07:57

I am using Builder plugin to create plugins and did field validations in my model in one of my plugins which works fine.

Let\'s say I have a validation something like t

3条回答
  •  既然无缘
    2021-01-24 08:23

    You can do something like this.

    1.- Disable thrown validations

    public $throwOnValidation = false;
    

    2.- Listen before Validation method but to do this trick we will implement an static flag to be secure to the method only will be called once and does not will be a recursive loop.

    public function beforeValidate()
    {
        static $called = false;
        if (!$called) {
            $called = true;
            if (!$this->validate()) {
                throw  new \October\Rain\Exception\ValidationException([
                    'Errors' => collect($this->validationErrors)->reduce(function (
                        $msg,
                        $error
                    ) {
                        return $msg . $error[0] . ' ';
                    })
                ]);
            }
        }
    }
    

    3.- Parse validations errors and concatenate into an string

    4.- By default October's flash message escape any HTML string to avoid XSS but if you really want to do that, you can override the e() laravel's helper function

    4.1.- Create a file inside your plugin directory called helpers.php and override the e() function

    4.2.- Append to the bootstrap.php file your file just before $helpersPath declaration, something like this:

    $customHelpers = __DIR__ . '/../plugins/vendor/yourplugin/helpers.php';
    if (file_exists($customHelpers)) {
        require $customHelpers;
    }
    

    Now you can append a
    in the reduce function like this

    return $msg . $error[0] . '
    ';

提交回复
热议问题