-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!',
    ]);
}
Wreigh
  • 3,215
  • 16
  • 37
php_boy
  • 41
  • 7
  • 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. – Tim Lewis Jul 09 '18 at 16:04
  • Replace `$request` with `$request->all()` in `$this->validate` – Derek Pollard Jul 09 '18 at 22:50
  • Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given... – php_boy Jul 09 '18 at 23:12
  • [Custom validation rule](https://laravel.com/docs/5.6/validation#custom-validation-rules)s can be used. Also see [this question](https://stackoverflow.com/questions/26121417/laravel-validation-exists-with-additional-column-condition-custom-validation) – iamab.in Jul 09 '18 at 23:12
  • I agree with @TimLewis, validation works with inputted data, not the data in the database. – Wreigh Jul 09 '18 at 23:27

2 Answers2

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.

Danoctum
  • 321
  • 5
  • 16
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@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 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.!

eSparkBiz Team
  • 110
  • 1
  • 5