Laravel 发送小数时遇到问题

Laravel having trouble sending decimals

我有一个 blade 需要价格的模板表格

              {!!Form::open(array('method'=>'POST','route'=>array('order.store'))) !!}

              <div class="form-group">

                {!! Form::label('price', 'Price:', ['class' => 'control-label']) !!}
                {!! Form::number('price', null, ['class' => 'form-control']) !!}
               </div>


               {!! Form::submit('Submit Order', ['class' => 'btn btn-primary']) !!}
                {!! Form::close() !!}

控制器获取价格并将其发送到电子邮件:

class OrderController extends Controller
{
 public function store()
    {
            $data=Input::all();
            //Validation rules
            $rules = array (


              'price'       => 'required|regex:/[\d]{2}.[\d]{2}/',
             );

     $validator  = Validator::make ($data, $rules);


             //If everything is correct than run passes.
        if ($validator -> passes()){

            //Send email using Laravel send function
            Mail::send('emails.order_received', $data, function($msg) use ($data)
            {
            //email 'From' field: Get users email add and name
                $msg->from($data['email'] , $data['owner']);
            //email 'To' field: change this to emails that you want to be notified.                    
                $msg->to('on@dx.com', 'Rn')->subject('New Order');

            });

            return Redirect::route('order.index')->with('flash_notice', 'Thank you');  
         }

        else
        {
            //return contact form with errors

            return Redirect::route('order.index')->withErrors($validator)->with('flash_error', 'This is not how one shops');
        }
    }
  }

table 被传递到电子邮件模板。

    <?php
//get the first name
$item = Input::get('item');
$manufacturer = Input::get ('manufacturer');
$price = Input::get('price');
$quantity = Input::get ('quantity');
$product_code = Input::get ('product_code');
$owner = Input::get ('owner');
$email = Input::get("email");
$created_at = Input::get("date");
?> 

每当我尝试添加价格时,即 (3.65),blade 表格会继续 return 整数错误消息。我的迁移将价格作为小数 (2,2) 我不明白为什么我的表单会抛出错误。任何帮助将非常感激。

P.S。除了正则表达式规则,我还尝试过浮点数和小数点。现在,如果我尝试使用正则表达式规则输入 2 而不是 02.00,它会根据正则表达式抛出错误。但是,如果我尝试遵守正则表达式规则,它需要一个整数(错误表示在 X 和 Y 之间)。

谢谢 M

首先,您肯定希望使用 numeric 规则进行验证。

其次,您正在使用 HTML5 输入字段 number,默认情况下它只接受整数,不接受浮点数。

如果您希望它也接受浮点数,因此不触发浏览器内置验证,请将您的代码更改为以下内容:

{!! Form::number('price', null, ['class' => 'form-control', 'step' => 'any']) !!}

或者,当然,您可以只使用 text 输入并自己进行内联验证。