在laravel 5.3中转换自定义验证
我已经创建了一个自定义验证,对于英语来说效果很好,但我无法找到在更多语言中实现这一点的方法 更改语言应显示所选语言中的错误 这是客户验证在laravel 5.3中转换自定义验证,laravel,translation,laravel-5.3,multilingual,laravel-validation,Laravel,Translation,Laravel 5.3,Multilingual,Laravel Validation,我已经创建了一个自定义验证,对于英语来说效果很好,但我无法找到在更多语言中实现这一点的方法 更改语言应显示所选语言中的错误 这是客户验证 <?php namespace App\Http\Validators; use Illuminate\Validation\Validator; class CustomValidator extends Validator { private $_custom_messages = array( "alph
<?php
namespace App\Http\Validators;
use Illuminate\Validation\Validator;
class CustomValidator extends Validator
{
private $_custom_messages = array(
"alpha_dash_spaces" => "The :attribute may only contain letters, spaces, and dashes.",
"alpha_num_spaces" => "The :attribute may only contain letters, numbers, and spaces.",
);
public function __construct( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) {
parent::__construct( $translator, $data, $rules, $messages, $customAttributes );
$this->_set_custom_stuff();
}
/**
* Setup any customizations etc
*
* @return void
*/
protected function _set_custom_stuff() {
//setup our custom error messages
$this->setCustomMessages( $this->_custom_messages );
}
/**
* Allow only alphabets, spaces and dashes (hyphens and underscores)
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateAlphaDashSpaces( $attribute, $value ) {
return (bool) preg_match( "/^[A-Za-z\s-_]+$/", $value );
}
/**
* Allow only alphabets, numbers, and spaces
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateAlphaNumSpaces( $attribute, $value ) {
return (bool) preg_match( "/^[A-Za-z0-9\s]+$/", $value );
}
}
我的语言环境中间件设置正确,我可以使用trans('myproject.validation.email')调用视图文件中的翻译
<?php
namespace App\Http\Middleware;
use App;
use Closure;
use Session;
class SetLocale
{
protected $languages = ['en', 'no'];
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Session::has('locale') && in_array(Session::get('locale'), $this->languages)) {
App::setLocale(Session::get('locale'));
}
return $next($request);
}
}
在配置文件中添加您的语言,并将该文件夹添加到lang目录中。您能解释一下吗?我已经准备好了语言目录,正如我在问题中提到的,翻译在视图中工作,但我无法为我的自定义验证方法找到方法,该方法只显示英语,而不考虑所选语言。在配置文件中添加您的语言,并在lang目录中添加该文件夹。可以吗请你再解释一下好吗?我已经准备好了语言目录,正如我在问题中提到的,翻译在视图中有效,但我无法为我的自定义验证方法找到方法,该方法只显示英语,而不考虑所选语言
<?php
namespace App\Http\Middleware;
use App;
use Closure;
use Session;
class SetLocale
{
protected $languages = ['en', 'no'];
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Session::has('locale') && in_array(Session::get('locale'), $this->languages)) {
App::setLocale(Session::get('locale'));
}
return $next($request);
}
}