Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 控制器中的Laravel 5验证_Php_Laravel_Laravel 5.3 - Fatal编程技术网

Php 控制器中的Laravel 5验证

Php 控制器中的Laravel 5验证,php,laravel,laravel-5.3,Php,Laravel,Laravel 5.3,我的控制器中有两种方法,我需要验证它,但我不知道如何验证 第一种应允许所有图像扩展的方法: public function testing(Request $request) { if($request->hasFile('img')) { $image = Input::file('img'); $filename = time() . '.' . $image->getClientOriginalExtension(); $

我的控制器中有两种方法,我需要验证它,但我不知道如何验证

第一种应允许所有图像扩展的方法:

public function testing(Request $request) {
    if($request->hasFile('img')) {
        $image = Input::file('img');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('images/' . $filename);
        Image::make($image->getRealPath())->resize(200, 200)->save($path);
        $file = $request->file('img');
        return ['url' => url('images/' . $filename)];
    }
}
第二种方法,只允许1个单词,如果有空格,则将其修剪为1个单词:

public function postDB(Request $request) {
    $newName = $request->input('newName');
    $websites = new Website();
    $websites->name = $newName;
    $websites->save();
    return redirect('template')->with('status', 'Website has been saved successfully!');
}

首先为您的数据编写新请求

php artisan make:request ImageRequest
然后写入图像请求

public function authorize()
{
    return true;
}

public function rules()
{
    return [
       'img' => 'file|image',
    ]    
}
public function testing(Requests\ImageRequest $request) {
    //for retrieving validation errors use:
      $imgErrors = $errors->first('img'); 
}
$validator = Validator::make(
            $image, [
                'img' => 'file|image',
            ]
        );
如果要自定义错误消息:

public function messages()
    {
        return [
            'img.image' => 'Some custom message ...',

        ];
    }
上次向方法注入请求(不要忘记使用App\Http\Requests):

关于表单请求验证

或者您可以使用验证程序facade(不要忘记使用验证程序):


关于关于可选字段的说明

第二个函数如何?与第一个方法类似,只需更改输入。如果有多个单词,我不知道如何修剪。请在验证后修剪它$newName=trim($request->input('newName'));我正在获取:Class App\ImageRequest不存在