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?
– durtto
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.
– Guilherme Rigotti
How the Migrations of
User
? In any case you can add to the model ofUser
an advisory function to get the field formatted the way you want it.– Isac
And how would I do that on the model? I’m new to Laravel.
– Guilherme Rigotti
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!
– novic
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.
– Guilherme Rigotti