-1

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!',
    ]);
}
5
  • That's not really how validation in Laravel works; it validates the input being sent to the server, not the values of the record found in the database. Unless the exists method does something different; never really used that. Commented Jul 9, 2018 at 16:04
  • Replace $request with $request->all() in $this->validate Commented Jul 9, 2018 at 22:50
  • Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given... Commented Jul 9, 2018 at 23:12
  • Custom validation rules can be used. Also see this question Commented Jul 9, 2018 at 23:12
  • I agree with @TimLewis, validation works with inputted data, not the data in the database. Commented Jul 9, 2018 at 23:27

2 Answers 2

0

I don't really know if exists:users,email,active,1,online,0 works. I would get the active or online field from the database and then return a message accordingly.

Sign up to request clarification or add additional context in comments.

Comments

0

Firstly, you need to provide your solution for FormRequest, which is as shown below in the snippet:

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 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.!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.