gpt4 book ai didi

php - 我如何自定义重置密码验证消息以及表单验证?

转载 作者:行者123 更新时间:2023-12-02 17:01:58 24 4
gpt4 key购买 nike

您好,我的 Web 模板默认语言是波斯语,目前,我将语言翻译保存在数据库中。所以我需要翻译所有表单验证消息。我像这样编辑了我的 RegisterController 验证消息并且它工作正常。

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Helpers\Translate;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default, this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/';

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
$messages = [
'name.required' => Translate::translate('Name_is_required.',session('locale')),
'name.max' => Translate::translate('The_name_may_not_be_greater_than_255_characters.',session('locale')),
'email.required' => Translate::translate('Email_is_required.',session('locale')),
'email.email' => Translate::translate('Type_valid_email.',session('locale')),
'email.unique' => Translate::translate('The_email_has_already_been_taken.',session('locale')),
'mobile.required' => Translate::translate('Mobile_is_required.',session('locale')),
'mobile.max' => Translate::translate('The_mobile_may_not_be_greater_than_15_characters.',session('locale')),
'mobile.regex' => Translate::translate('Type_valid_contact_number.',session('locale')),
'password.required' => Translate::translate('Password_is_required.',session('locale')),
'password.min' => Translate::translate('The_password_must_be_at_least_6_characters.',session('locale')),
'password.confirmed' => Translate::translate('The_password_confirmation_does_not_match.',session('locale')),
];
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'mobile' => ['required', 'string', 'max:15','regex:/^(00|\+|0)[1-9]{1}([0-9][\s]*){6,16}$/'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
],$messages);
}

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'password' => Hash::make($data['password']),
]);
}
}

我还想更改 ForgotPasswordController 验证消息,但我无法更改,它无法正常工作,也没有显示任何错误

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Helpers\Translate;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;

class ForgotPasswordController extends Controller
{

/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset emails and
| includes a trait which assists in sending these notifications from
| your application to your users. Feel free to explore this trait.
|
*/

use SendsPasswordResetEmails;

/**
* Create a new controller instance.
*
* @return void
*/

public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
$messages = [
'email.required' => Translate::translate('Email_is_required.',session('locale')),
'email.email' => Translate::translate('Type_valid_email.',session('locale')),
];
return Validator::make($data, [
'email' => ['required', 'string', 'email', 'max:255'],
],$messages);
}
}

如何更改重置密码的验证消息???我是 laravel 的新手

谁能帮帮我?

最佳答案

SendsPasswordResetEmails trait 在 ForgotPasswordController 中使用,它有一个方法 validateEmail 来处理验证规则及其消息。如果您想更改验证及其消息,请覆盖 ForgotPasswordController 中的两个函数。

像这样更改您的代码:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Helpers\Translate;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;

class ForgotPasswordController extends Controller
{

/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset emails and
| includes a trait which assists in sending these notifications from
| your application to your users. Feel free to explore this trait.
|
*/

use SendsPasswordResetEmails;

/**
* Create a new controller instance.
*
* @return void
*/

public function __construct()
{
$this->middleware('guest');
}
/**
* Validate the email for the given request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateEmail(Request $request)
{
$this->validate($request,
[
'email' => 'required|email'
],
$messages = [
'email.required' => Translate::translate('Email_is_required.',session('locale')),
'email.email' => Translate::translate('Type_valid_email.',session('locale')),
]);
}
}

希望你能理解。

更新

同样的,ResetsPasswords trait被ResetPasswordController使用,所以你可以在controller中覆盖traits的功能。

只需将以下函数放在ResetPasswordController

/**
* Get the password reset validation rules.
*
* @return array
*/
protected function rules()
{
// If you have further fields and rules you can add in following array.
return [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:6',
];
}

/**
* Get the password reset validation error messages.
*
* @return array
*/
protected function validationErrorMessages()
{
return [
// Here write your custom validation error messages
];
}

希望你能理解。如果有任何困惑,请随时询问。

关于php - 我如何自定义重置密码验证消息以及表单验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53737493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com