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();
});
you’re making
unique
in different tables, which is the error screen?– novic
@Virgilionovic Actualzado !
– Allan Ramos
Can you also put the control part? Maybe the error is in the code of the controler
– novic
What
new Cpf
ago?– Wallace Maxters
It validates the CPF. This function works, because if you type the invalid Cpf it returns an error message pro form.
– Allan Ramos