我已经创建了一个适用于英语的自定义验证,但我无法找到用更多语言执行此操作的方法。
更改语言应以所选语言显示错误。
这是自定义验证
<?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 );
}
}
我还将validation.php复制到了它的语言文件夹&resources / lang / no / validation.php&#39;并且还将文件中的验证定义为
return array(
'alpha_dash_spaces' => 'Other language',
'alpha_num_spaces' => 'Other language',
我的语言环境中间件设置正确,我可以使用trans(&#39; myproject.validation.email&#39;)来调用视图文件中的翻译
<?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);
}
}