OctoberCMS插件可一次显示所有验证 [英] OctoberCMS plugin show all the validations at once

查看:155
本文介绍了OctoberCMS插件可一次显示所有验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Builder 插件创建插件,并在模型中一次完成了字段验证我的插件效果很好.

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

假设我在我的插件中将这样的验证称为 Team .

Let's say I have a validation something like this in one my plugin call it as Team.

模型文件: technobrave \ team \ models \ Team.php

<?php namespace Technobrave\Team\Models;

use Model;

/**
 * Model
 */
class Team extends Model
{
    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [
    'name' => 'required|unique:technobrave_team_',
    'photo' => 'required',    
    'description'=>'max:1000',
    'position' => 'required',            
    'phone' => 'required',            
    'mobile' => 'required',            
    'email' => 'required|email|unique:technobrave_team_',            
    'website' => 'url',            


    ];


    public $customMessages = [
                'name.required' => 'Please enter team member name',
                'name.unique' => 'This team member name already exists',
                'photo.required' => 'Please select team member photo',
                'description.max' => 'Please enter maximum 1000 characters for description',              
                'position.required' => 'Please enter team member position',                
                'phone.required' => 'Please enter team member phone number',  
                'mobile.required' => 'Please enter team member mobile number',  
                'email.required' => 'Please enter team member email address',  
                'email.email' => 'Please enter valid team member email address', 
                'email.unique' => 'This email address already exists', 
                'website.url' => 'Please enter valid team member url', 




    ];

}

这绝对好用,我可以看到验证,但它们又是一个接一个.相反,我希望他们一次全部提出.对于所有领域.

This works absolutely fine, I am able to see the validations, but they are coming one after another. Instead I want them to come up all at once. For all the fields.

这可能吗?我怎样才能做到这一点?

Is this possible ? How can I accomplish this ?

谢谢

推荐答案

好的,这是我解决此问题的方法.

Ok Guys, Here is how I resolved this issue.

只需转到要使用的插件,然后打开其 Plugin.php 文件,然后添加以下代码行即可.

Simply go to your plugin which you want to work on and open its Plugin.php file and add following lines of code.

Plugin.php

public function boot()
    {
        Event::listen('backend.page.beforeDisplay', function($controller, $action, $params) {
      /* Here you can put your css file wherever you want  .. I put in my current theme's directory */
      $controller->addCss('/themes/your_current_theme_folder_name/assets/css/general.css'); 
        }); 
    }

在将class代码添加到此文件之前,请忘记添加use Event;.

Done forget to add use Event; before you add your class code in this file.

打开 general.css 文件,并将其放在下面的代码中.

Open general.css file and put below code.

.flash-message.fade.in {
white-space: pre;
} 

下一步打开插件的模型文件,并将其放在下面的代码中.

Next Open plugin's model file and put below code.

Team.php (模型文件)

public $throwOnValidation = false;


    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] . "\r\n"; 
                        })
                ]);
            }
        }
    }

希望这会有所帮助.

这篇关于OctoberCMS插件可一次显示所有验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆