Validate Laravel Single Field

Asked

Viewed 1,254 times

0

Speak guys, I’m trying to learn Laravel and I’m currently caught in a seemingly simple thing, is the following.

I created a very simple system with login and other things, and I’m trying to implement the editing of the logged-in user data, I made the validations and defined that the email is.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UsersRequest extends FormRequest
{
public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'type' => 'required|max:1'
    ];
}

public function messages(){
    return [
        'required' => 'O campo ":attribute" é obrigatório!',
        'numeric' => 'O campo ":attribute" deve ser um número!',
        'min' => 'O campo ":attribute" deve ter no mínimo :min caracteres!',
        'max' => 'O campo ":attribute" deve ter no maximo :max caracteres!',
        'type.required' => 'O campo "tipo" é obrigatório!',
        'unique' => 'Este ":attribute" não se encontra disponivel no momento!'
    ];
}
}

So far so good, the validation works correctly, however, when I try to update the record of some user, for not having changed the email of the same one is returned me the error, how can I make so that the validation of the email opens an exception for the user of that ID?

While I’m at it, I was wondering if this form of validation is indicated, correct, safe, efficient and things like that, you would have another way to recommend me?

2 answers

1

I found the solution Guys, I’ll make it available in case anyone else has this problem.

In my case, I use the Laravel 5.4 and validate my requests with Form Request, so it didn’t work as in the examples of the documentation, but just adapt that worked well. The code was as follows.

<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Http\FormRequest;

class UsersRequest extends FormRequest
{    
public function authorize()
{
    return true;
}

public function rules()
{
    //$user = User::find($this->user);
    return [
        'name' => 'required|max:255',
        'email' => ['required', Rule::unique('users')->ignore(Auth::user()->id)],
        'type' => 'required|max:1'
    ];
}

public function messages(){
    return [
        'required' => 'O campo ":attribute" é obrigatório!',
        'numeric' => 'O campo ":attribute" deve ser um número!',
        'min' => 'O campo ":attribute" deve ter no mínimo :min caracteres!',
        'max' => 'O campo ":attribute" deve ter no maximo :max caracteres!',
        'type.required' => 'O campo "tipo" é obrigatório!',
        'unique' => 'Este ":attribute" não se encontra disponivel no momento!'
    ];
}
}

-1

Add in your HTML the values that are returned by the database.

Ex: if I am editing the email field in the bank with value [email protected], in my HTML the field should come with [email protected].

So, when sending a POST with the form data, even if you have not edited any field the object goes to the server with the data.

  • But I do this, value="{{Auth::user()->email}}"> the problem is that the email that is in the user can theoretically not be saved in the database because it already exists, what I want to do is make an exception for a certain ID

  • Got it, good you got it fixed :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.