Get formatted values from custom Form Request - Laravel 5.6

1.7k Views Asked by At

I`m using custom form request to validate all input data to store users.

I need validate the input before send to form request, and get all validated data in my controller.

I have a regex function ready to validate this input, removing unwanted characters, spaces, allow only numbers and etc.

Would to get all data validated in controller, but still have no success.

Input example: 

$cnpj= 29.258.602/0001-25

How i need in controller:

$cnpj= 29258602000125

UsuarioController

class UsuarioController extends BaseController
{
    public function cadastrarUsuarioExterno(UsuarioStoreFormRequest $request)
  {
     //Would to get all input validated - no spaces, no!@#$%^&*, etc 
     $validated = $request->validated();

     dd($data);
  }
...
}

UsuarioStoreFormRequest

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;

class UsuarioStoreFormRequest 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()
{

    return [
        'cnpj' => 'required|numeric|digits:14',
    ];
}

Custom function to validate cnpj

function validar_cnpj($cnpj)
{
$cnpj = preg_replace('/[^0-9]/', '', (string) $cnpj);
// Valida tamanho
if (strlen($cnpj) != 14)
    return false;
// Valida primeiro dígito verificador
for ($i = 0, $j = 5, $soma = 0; $i < 12; $i++)
{
    $soma += $cnpj{$i} * $j;
    $j = ($j == 2) ? 9 : $j - 1;
}
$resto = $soma % 11;
if ($cnpj{12} != ($resto < 2 ? 0 : 11 - $resto))
    return false;
// Valida segundo dígito verificador
for ($i = 0, $j = 6, $soma = 0; $i < 13; $i++)
{
    $soma += $cnpj{$i} * $j;
    $j = ($j == 2) ? 9 : $j - 1;
}
$resto = $soma % 11;
return $cnpj{13} == ($resto < 2 ? 0 : 11 - $resto);
}
3

There are 3 best solutions below

2
On BEST ANSWER

You could use the prepareForValidation method in your FormRequest. This way your input would be modified and replaced in the request before it is validated and you can normally retrieve it in the controller with $request->get('cnpj'); after the validation was successful.

public function prepareForValidation()
{
    $input = $this->all();

    if ($this->has('cnpj')) {
        $input['cnpj'] = $this->get('cnpj'); // Modify input here
    }

    $this->replace($input);
}
4
On

You could extend the validated function in UsuarioStoreFormRequest as follows

/**
 * Get the validated data from the request.
 *
 * @return array
 */
public function validated()
{
    $validated = parent::validated();

    //Add here more characters to remove or use a regex
    $validated['cnpj'] = str_replace(['-', '/', ' ', '.'], '', $validated['cnpj']);

    return $validated;
}
2
On

Do it like this:

$string = "29.258.602/0001-25";
preg_match_all('!\d+!', $string, $matches);
return (implode("",$matches[0])); 

Hope its help