Skip to content

Instantly share code, notes, and snippets.

@bergpb
Last active February 28, 2020 14:51
Show Gist options
  • Select an option

  • Save bergpb/611ecfb230bef1c03d937e7e0aa9c451 to your computer and use it in GitHub Desktop.

Select an option

Save bergpb/611ecfb230bef1c03d937e7e0aa9c451 to your computer and use it in GitHub Desktop.
laravel erro na validacao de um campo
##### form request
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PessoaFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$id = $this->segment(2);
return [
'nome' => 'max:100|nullable',
'cpf' => "required|min:11|max:14|unique:pessoas,cpf,{$id},id",
'identidade' => "min:8|max:14|unique:pessoas,identidade,{$id},id",
'cep' => 'max:9|nullable',
'end_residencial' => 'max:100|nullable',
'bairro' => 'max:50|nullable',
'cidade' => 'max:50|nullable',
'uf' => 'max:2|nullable',
'profissao' => 'max:30|nullable',
'fone' => 'max:14|nullable',
'celular' => 'max:15|nullable',
];
}
public function messages()
{
return [
'cpf.required' => 'Campo CPF deve ser preenchido.',
'cpf.max' => 'Campo CPF deve conter no máximo 14 números.',
'cpf.min' => 'Campo CPF deve ter no mínimo 11 números.',
'cpf.unique' => 'Campo CPF já cadastrado.',
'identidade.unique' => 'Campo Número Identidade já cadastrado.',
'identidade.max' => 'Campo Número Identidade deve conter no máximo 14 números.',
];
}
}
##### controller
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\PessoaFormRequest $request
* @return \Illuminate\Http\Response
*/
public function store(PessoaFormRequest $request)
{
Pessoa::create($request->all());
return redirect()->route('index-pessoas');
}
#### migration
public function up()
{
Schema::create('pessoas', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nome', 100)->nullable();
$table->string('cpf', 11)->unique();
$table->string('identidade', 14)
->unique()
->nullable();
$table->string('cep', 8)->nullable();
$table->string('end_residencial', 100)->nullable();
$table->string('bairro', 50)->nullable();
$table->string('cidade', 50)->nullable();
$table->string('uf', 2)->nullable();
$table->string('profissao', 30)->nullable();
$table->string('fone', 10)->nullable();
$table->string('celular', 11)->nullable();
$table->timestamps();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment