Back

Laravel 5.5 Validation Message


Leo Yanes posted 1 week ago

I have a method:

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!',
        ]);
    }
Roman replied 1 week ago

Well, first - You should provide Your solution a FormRequest, which might be as the following:


    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 protected]|email_online:[email protected]',
                '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 and email_online rules don't exist on the Laravel box. You should create them via php artisan make:rule EmailActiveRule and php artisan make:rule EmailOnlineRule.

Then, You need to extend Your rules by following this article.

Then, in the rules You need to check the records in the database via Repository or Model


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us