l5-repository Package Validators

1

1st Is it possible to validate using the package itself? Currently, I use the package just to return the rules, eg:

Validator::make($data, $this->getRules())->validate();

Onde:

$data:
São os meus dados que vieram do HTML.

$this->getRules()
Esse método getRules retornar minhas regras, é possível informar também se estou criando um dado ou atualizando ($action).

2º Is it possible to format the data before it is valid ?. When I did the Form Request, I did the following, for example:

public function validationData(){
     $data = $this->all();

     if(array_key_exists('valor_total', $data)){
         if (preg_match("/[,]/", $data['valor_total']) > 0){
            $data['valor_total'] = preg_replace('/[.]/', '', $data['valor_total']);
            $data['valor_total'] = preg_replace('/[,]/', '.', $data['valor_total']);
            $data['valor_total'] = (float)$data['valor_total'];
         }
     }

    $this->getInputSource()->replace($data);
    return $data;
}

3rd Is it possible to change a data after validation ?. For example, I have a field called date_name (date) that I get from the front end in the format d / m / Y and in the validation I check this same format, I want to after validation, format for Y-m-d. I know I can do this with mutators, but I would like to do this with Validate.

4º How to do Unique type validations? With the Form Request, I would do the following:

public function rules(){
    $id = $this->route('filiai');

    return [
      'matriz_id' => 'required|integer|exists:pessoas_juridicas,id',
      'cnpj' => "required|cnpj|unique:empresas_filiais,cnpj,$id,id",
    ];
}

Only now with l5-repository, I will not have more to request, so how should I do it?

Does anyone know of a project (preferably with as many real examples as possible) in a public repository to build on?

    
asked by anonymous 28.11.2017 / 13:17

1 answer

1

First create the validation class:

Theninyourrepositoryregisterthisvalidator:

Nowwhenyoucreatetheregistrybytherepositoryorupdate,thevalidatorswillautomaticallyrun.

$this->attributeRepository->create($data)$this->attributeRepository->update($data,$id)

Ifyouneedtorunmanually:

Onuniquevalidation,italreadyidentifiestheuniqueattributeyouarecreatingorchanging.

Aboutchangingthedatabeforeinsertingintothedatabaseyoucanuse Mutators .

    
28.11.2017 / 16:28