Laravel 5.5验证消息 [英] Laravel 5.5 Validation Message

查看:76
本文介绍了Laravel 5.5验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法:

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        'email' => 'required|string|exists:users,email,active,1,online,0',
        'password' => 'required|string',
    ], [
        'password.required' => 'Password required!',
        'email.exists' => 'Email not found!',
    ]);
}

验证用户"表中的数据库中是否存在具有特定电子邮件地址的用户,该用户处于活动状态且未登录. 用户"表的架构如下:

Which validates that in the database within the 'users' table there is a user with a certain email address, that the user is active and not logged in. The schema of the 'users' table is the following:

Schema::create($this->table, function (Blueprint $table) {
    $table->increments('id');
    $table->string('first_name');
    $table->string('last_name');
    $table->string('email')->unique();
    $table->string('password');
    $table->boolean('active')->default(true);
    $table->boolean('online')->default(false);
    $table->timestamps();
});

我想根据用户状态(在线|活动)返回错误消息

I want to return an error message depending on the user's status (online | active)

你能告诉我我该怎么做吗? 我尝试过:

Could you tell me how I could do? I have tried with:

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        'email' => 'required|string|exists:users,email,active,1,online,0',
        'password' => 'required|string',
    ], [
        'password.required' => 'Password required!',
        'email.exists' => 'Email not found!',
        'email.active' => 'User not active!',
        'email.online' => 'Email is onlone!',
    ]);
}

推荐答案

首先,您需要为FormRequest提供解决方案,如下面的代码段所示:

namespace App\Http\Requests;

use Illuminate\Support\Facades\Auth;

/**
 * Class UserRequest
 * @package App\Http\Requests
 */
class UserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return !Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required|unique:users|email_active:email@me.com|email_online:email@me.com',
            'password' => 'required|string|between:4,40'
        ];
    }

    public function messages()
    {
        return [
            'email.required' => 'Email is required',
            'email.unique' => 'This Email is already in use',
            'email.email_active' => 'This email is not active',
            'email.email_online' => 'This email is online',
            'password.required' => 'Password is required',
            'password.between' => 'Password length should be between :min and :max characters'
        ];
    }
}

email_active和email_online是两个不适用于Laravel框的规则.您应该通过php artisan make:rule EmailActiveRule和php artisan make:rule EmailOnlineRule创建它们.

email_active and email_online are two rules which are not applicable Laravel box. You should create them via php artisan make:rule EmailActiveRule and php artisan make:rule EmailOnlineRule.

这样做之后,您需要按照此答案的步骤来扩展规则

After doing that, you need to extend your rules by following the procedure of this answer

形成规则后,您需要通过存储库或模型检查数据库中的记录.

Once the rules are formed, You need to check the records in the database via Repository or Model.

希望此解决方案对您有用.

Hope this solution works for you.!

这篇关于Laravel 5.5验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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