Php 如何在输入中以小数形式显示价格,但store()上没有整数取值错误?
我有下面的jQuery,它可以工作,当在价格输入中插入一个值时,例如“3”,它加上“3,00”,但是控制器中有一个验证错误,表示价格是整数。你知道如何纠正这个问题吗?也就是说,将价格维护为整数,但不存在验证错误 后置控制器:Php 如何在输入中以小数形式显示价格,但store()上没有整数取值错误?,php,jquery,laravel,Php,Jquery,Laravel,我有下面的jQuery,它可以工作,当在价格输入中插入一个值时,例如“3”,它加上“3,00”,但是控制器中有一个验证错误,表示价格是整数。你知道如何纠正这个问题吗?也就是说,将价格维护为整数,但不存在验证错误 后置控制器: $prod = Product::create([ 'price' => $request->price, ]); 输入: <div class="form-group col-md-6"> <label for="pric
$prod = Product::create([
'price' => $request->price,
]);
输入:
<div class="form-group col-md-6">
<label for="price">Price></label>
<input type="number" min="1" step="any" required class="form-control" value="{{ old('price', '0.00€') }}" name="price" id="price"/>
</div>
如果希望将用户的原始输入数据发送到后端,同时在前端(在同一输入框中)显示其数据格式,则可以使用隐藏的表单字段,用户输入值后即可设置该字段
<div class="form-group col-md-6">
<label for="price">Price></label>
<input type="hidden" name="price" />
<input type="number" min="1" step="any" required class="form-control" value="{{ old('price', '0.00€') }}" name="show-price" id="price"/>
</div>
我不确定在#show price上输入的内容是什么,因此您可能需要对其进行一些格式化,以将其修剪为一个裸整数,但其思想是,在执行可视化格式化之前,您可以将隐藏的表单字段值设置为该原始数据
<div class="form-group col-md-6">
<label for="price">Price></label>
<input type="hidden" name="price" />
<input type="number" min="1" step="any" required class="form-control" value="{{ old('price', '0.00€') }}" name="show-price" id="price"/>
</div>
document.getElementById("show-price").onblur =function (){
var $price = document.getElementById("price");
$price.value = this.value;
this.value = parseFloat(this.value.replace(/,|\€/g, ""))
.toFixed(2)
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}