Validate Single Field Laravel

0

Talk to people, I'm trying to learn Laravel and I'm currently caught up in something apparently simple, it's the following.

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

<?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 registry of some user, for not having changed the email of the same is returned the error, how can I make the unique validation of the email open an exception for the user of that ID?

I would like to know if this form of validation is indicated, correct, safe, efficient and the like, would you have another way to recommend me?

    
asked by anonymous 29.04.2017 / 01:29

2 answers

0

Adds in your HTML the values that are returned by the bank.

Ex : If I am editing the email field in the database that is worth [email protected] , in my HTML the field should come with [email protected] .

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

    
29.04.2017 / 02:35
0

I found the solution Galera, I will make it available in case anyone else has this problem.

In my case, I use Laravel 5.4 and validated my requests with Form Request, so it did not work as in the documentation examples, but it was only adapting that worked perfectly. The code is 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!'
    ];
}
}
    
29.04.2017 / 02:53