Laravel Validation Unique

Asked

Viewed 4,203 times

2

I have a registration form, and in it there is information for popular two tables:

  • User(User);
  • Shipper(Sender).

I created the Request Laravel to isolate the controller form validation rule. Cloquei unique in two places, being the first in the field email user, and the second in document of the person.

Problem

Validation only works for the first case, while it does not apply for the second. The result is to take an Exception in the face if the guy’s number matches and try to register.

https://image.ibb.co/kaTRBK/img.png

Controller

class RegisterSenderPersonController extends Controller
{
    public function store(SenderPersonUserStoreRequest $request)
    {
        $sender = Sender::create([
            'document' => $request['document'],
            'zipcode' => $request['zipcode'],
            'telephone' => $request['telephone'],
            'birthday' => $request['birthday'],
            'gender' => $request['gender'],
            'city_id' => $city->id,
        ]);

        if($sender->id) {
            $user = User::create([
                'name' => $request['name'],
                'email' => $request['email'],
                'sender_id' => $sender->id,
                'password' => $request['password'],
            ]);

            return 'gravou';
        }

        return 'deu ruim';
    }
}

Senderrequest

return [
            'birthday' => 'nullable|date',
            'document' => ['unique:sender', 'required', 'min:11','max:14', new Cpf],
            'email' => 'unique:user|required|min:4|max:35',
            'gender' => 'nullable|boolean',
            'name' => 'required|min:4|max:30',
            'password' => 'required|min:8|max:20',
            'telephone' => 'required|min:11|max:14',
            'zipcode' => 'required|min:8|max:9',
        ]; 

Sendermigration

Schema::create('sender', function (Blueprint $table) {
            $table->increments('id');
            $table->string('company_name', 60)->nullable()->unique();
            $table->string('exhibition_name', 25)->nullable();
            $table->string('document', 14)->unique();
            $table->date('birthday')->nullable();
            $table->boolean('gender')->nullable();
            $table->string('zipcode', 8);
            $table->string('telephone', 11);
            $table->boolean('active')->default(true);
            $table->timestamps();
        });
  • 1

    you’re making unique in different tables, which is the error screen?

  • @Virgilionovic Actualzado !

  • Can you also put the control part? Maybe the error is in the code of the controler

  • What new Cpf ago?

  • It validates the CPF. This function works, because if you type the invalid Cpf it returns an error message pro form.

1 answer

2


According to the Laravel documentation, it is necessary to pass another column parameter in the Rules

Unique:table, column, except,idColumn

The field under validation must be Unique in a Given database table. If the column option is not specified, the field name will be used.

Try this:

return [
        'birthday' => 'nullable|date',
        'document' => ['unique:sender,document', 'required', 'min:11','max:14', new Cpf],
        'email' => 'unique:user,email|required|min:4|max:35',
        'gender' => 'nullable|boolean',
        'name' => 'required|min:4|max:30',
        'password' => 'required|min:8|max:20',
        'telephone' => 'required|min:11|max:14',
        'zipcode' => 'required|min:8|max:9',
    ]; 
  • I’ve changed, but it remains the same

Browser other questions tagged

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