2015-11-04 67 views
0

我有一個刀片模板形式,需要一個價格Laravel無法發送小數

   {!!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('[email protected]', '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'); 
     } 
    } 
    } 

表傳遞到電子郵件模板。

<?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)時,刀片形式會繼續返回一個整數的錯誤消息。我的遷移將價格作爲小數(2,2)我無法理解我的表單拋出錯誤的原因。任何幫助將非常感激。

P.S.除了正則表達式規則,我嘗試了float和decimal。現在,如果我嘗試使用正則表達式規則輸入2而不是02.00,它將引發基於正則表達式的錯誤。但是,如果我嘗試遵守正則表達式規則,它需要一個整數(X和Y之間的錯誤)。

感謝 中號

回答

1

首先,你肯定會想使用numeric規則您的驗證。

其次,您使用的HTML5輸入字段number默認情況下只接受整數,而不是浮動。

如果你希望它也接受浮動,因此不會觸發內置的驗證瀏覽器更改您的代碼如下:

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

或者,當然,你可以只使用一個text輸入並執行內聯驗證你自己。

+0

感謝您的快速回復添!當我嘗試使用float驗證時,我收到一個Method [validateFloat]不存在的錯誤。我正在使用Laravel 5 btw。 – MRF

+0

對不起,我的壞。我想到的驗證規則在Laravel中名爲'numeric'。 – Tim