Format data before saving to the database

Asked

Viewed 207 times

3

I’m using the Laravel to develop a system, but I have a problem with data coming from a form, I need to format a data in the following format (#.##) to perform certain calculations, the problem that the value comes with the following format (###.#) making the final value go wrong, don’t know how I can format this data on Controller or before sending it.

Follow the input code

<label for="peso">Peso (Kg) : </label>
<input type="text" name="peso" class="form-control" v-model="peso"/>
<label for="altura">Altura (m) : </label>
<input type="text" name="altura" class="form-control" v-model="altura" v-mask="'#.##'"/>
<br/>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" value="Enviar" class="btn btn-success pull-right"/>
<br>

Follow the Controller code where I need to validate the data

public function store(ImcRequest $request)
{
    $imc = new Imc;
    $imc->peso = $request->peso;
    $imc->altura = $request->altura;
    $altura2 = ($imc->altura) * ($imc->altura);
    $imc->imccalculado = ($imc->peso / $altura2);

    if (($imc->imccalculado) < 16.00) {
        $imc->classificacao = 'Magreza severa';
    } elseif (($imc->imccalculado >= 16.00) && ($imc->imccalculado <= 16.99)) {
        $imc->classificacao = 'Magreza moderada';
    } elseif (($imc->imccalculado >= 17.00) && ($imc->imccalculado <= 18.40)) {
        $imc->classificacao = 'Magreza leve';
    } elseif (($imc->imccalculado >= 18.5) && ($imc->imccalculado <= 24.99)) {
        $imc->classificacao = 'Normal';
    } elseif (($imc->imccalculado >= 25.00) && ($imc->imccalculado <= 29.99)) {
        $imc->classificacao = 'Preobeso';
    } elseif (($imc->imccalculado >= 30.00) && ($imc->imccalculado <= 34.99)) {
        $imc->classificacao = 'Obesidade leve';
    } elseif (($imc->imccalculado >= 35.00) && ($imc->imccalculado <= 39.99)) {
        $imc->classificacao = 'Obesidade media';
    } elseif (($imc->imccalculado) >= 40.00) {
        $imc->classificacao = 'Obesidade morbida';
    }

    $imc->user_id = Auth::id();
    $imc->save();

    return redirect('/imcs');
}
  • Where does the information? from the database? you can change the mask in the database, it would not be the case?

  • The information the user informs in a form, before saving in the bank I perform a calculation, the value came with the wrong format in the form he ends up saving wrong in the bank.

  • How the Migrations of User ? In any case you can add to the model of User an advisory function to get the field formatted the way you want it.

  • And how would I do that on the model? I’m new to Laravel.

  • William are you sending this with Ajax? by Angular ? and the format would be in v-model Height? has how to tell how you are sending and this included in your question!

  • I am submitting the form by the POST method, the v-model was a test that I did and it didn’t work, I am using Vue to display the user data, but this is done only after the calculation is done.

Show 1 more comment

1 answer

1


Try to use:

$imc->peso = number_format($request->peso, 2, '.');
$imc->altura = number_format($request->altura, 2, '.');

Another option of gambiarra:

$num = 100.4;
$num = str_replace('.', '', $num);
$num = str_replace(',', '', $num);
echo substr($num, 0, strlen($num)-2).".".substr($num, strlen($num)-2);

I believe it is better to treat 3-digit numbers with an error message or mask in the form, but these are examples of gambiarras for treatment on controller. A tip, check how the data arrive in controller before, put a dd($request->campoX); at the beginning of the function code because the problem may be in some component loaded in the page JS as well.

Browser other questions tagged

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