laravel 5.1中的验证错误(数组到字符串转换)

时间:2015-12-16 12:22:02

标签: laravel-5 laravel-5.1

我正在努力解决这个错误几个小时,但没有运气,我需要有关此错误的帮助总是说: (数组转换为字符串)

代码:

<?php 
namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Hash, Auth, URL, Route, Cart, View, Paypal;
use App\Product, App\ProductBenefit, App\Country, App\Currency, App\User, App\City;

class HomeController extends BaseController {
public function postCheckoutStepPayment(Request $request){
        if(!is_null($request->input('ship_to_diff_address'))){
            $validate = Validator::make($request->all(), User::$rules);
            if($validate->fails()) { //<- problem this part
               return 'failed';
            }

        }
   }
}
  

user.php的

public static $rules = array(
             'diff_firstname' => 'required',
             'diff_lastname'  => 'required',
             'diff_phone'     => 'required',
             'diff_countries' => 'required',
             'diff_city'      => 'required',
             'diff_state'     => 'required',
             'diff_address'   => 'required',
             );

enter image description here

3 个答案:

答案 0 :(得分:1)

看来config / app.php中locale的值是一个数组,而vendor / laravel / framework / src / Illuminate / Translation / FileLoader.php中的函数loadPath期望它是一个字符串。 所以我建议你在配置文件中将它的值设置为'en'或'sv',然后根据需要在代码中以编程方式更改它。

答案 1 :(得分:0)

在您的控制器中添加此类

use App\Http\Requests;

在刀片文件中尝试此代码。

 @if(isset($errors))
     <ul style="list-style: none;" class="alert alert-warning">
     @foreach($errors->all() as $content)
         <li>{{$content}}</li>
     @endforeach
     </ul>
 @endif

答案 2 :(得分:0)

在顶部添加use Redirect,然后

 public function postRegister(Request $request)
        {
            $v = Validator::make($request->all(), [
             'firstname' => 'required',
             'lastname'  => 'required',
             'phone'     => 'required',
             'countries' => 'required',
             'city'      => 'required',
             'state'     => 'required',
             'address'   => 'required',
            ]);

            if ($v->fails()) { 
                return redirect::to('register')
                            ->withErrors($v->messages())
                            ->withInput();
            }


        }