问题
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 this in one my plugin call it as Team.
Model File: 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 ?
Thanks
回答1:
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
<?php
function e($str)
{
return $str;
}
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] . '<br>';
回答2:
Ok Guys, Here is how I resolved this issue.
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');
});
}
Done forget to add use Event;
before you add your class
code in this file.
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 (Model File)
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";
})
]);
}
}
}
Hope this helps.
来源:https://stackoverflow.com/questions/43756590/octobercms-plugin-show-all-the-validations-at-once