Laravel验证不会返回错误 [英] Laravel Validation not returning error

查看:431
本文介绍了Laravel验证不会返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的laravel控制器:

  public function store()
{
$ validator = Validator :: make($ data = Input :: all(),City :: $ rules);
if($ validator-> fails())
{
return Redirect :: back() - > withErrors($ validator) - > withInput();

}
$ image_temp = Input :: file('image');
$ name = Input :: file('image') - > getClientOriginalName();
$ data ['image'] ='';
if(Image :: make($ image_temp-> getRealPath()) - > save('public / up / city /'.$ name)){
$ data ['image'] =上/城市/'.$名;
}
City :: create($ data);
return Redirect :: route('admin.cities.index');
}

这是我的模型:

  class City extends \ Eloquent {
protected $ primaryKey ='city_id';

//在这里添加验证规则
public static $ rules = [
'title'=> 'required',
'image'=> 'mimes:jpeg',
'parent_id'=> 'required',
'name'=> 'required',
'english_name'=>'unique:cities,english_name | required'

];
//不要忘记填充这个数组
protected $ fillable = ['name','parent_id','english_name','population','phone_prefix','image'];
}

我有一个表单我使用 {{$当我发送没有填充输入的表单时,我的表单输入为下面的错误 - > first('inputName','< p class =error>:message< / p>))}} 每个表单输入都会出现错误。但是,当我填写所有表单输入,然后提交Laarvel验证返回失败(我的意思是大规模分配不工作,不注册,并重新导向回创建页面。)有什么问题?

解决方案

几乎总是出现批量分配错误的原因是 $ fillable 数组。你的情况是 title

  protected $ fillable = [' title','name','parent_id','english_name','population','phone_prefix','image']; 



编辑



显然这个问题实际上是 $ rules 数组中的 title ,它应该是名称 ...


I am new in Laravel.

This is my laravel controller:

public function store()
    {
        $validator = Validator::make($data = Input::all(), City::$rules);
        if ($validator->fails())
        {
            return Redirect::back()->withErrors($validator)->withInput();

        }
        $image_temp=Input::file('image');
        $name = Input::file('image')->getClientOriginalName();
        $data['image']='';
        if(Image::make($image_temp->getRealPath())->save('public/up/city/'.$name)){
            $data['image']='up/city/'.$name;
        }
        City::create($data);
        return Redirect::route('admin.cities.index');
    }

and This is my model:

class City extends \Eloquent {
   protected $primaryKey='city_id';

    // Add your validation rules here
    public static $rules = [
         'title'     => 'required',
         'image'     => 'mimes:jpeg',
         'parent_id' => 'required',
         'name'      => 'required',
         'english_name'=>'unique:cities,english_name|required'

    ];
    // Don't forget to fill this array
    protected $fillable = ['name', 'parent_id', 'english_name','population','phone_prefix','image'];
}

And I have a form I use {{ $errors->first('inputName','<p class="error">:message</p>') }} bellow my form inputs, when I send form without filling inputs I get error under each form input. But when I fill out all form inputs and then submit the Laarvel validation return fail (I mean mass assignment not working and not registering, and redirects back to create page.) what is the problem?

解决方案

Almost always the reason for a mass assignment error is a missing attribute in the $fillable array. In your case it is title.

protected $fillable = ['title', 'name', 'parent_id', 'english_name','population','phone_prefix','image'];
                          ^

Edit

Apparently the problem was actually that the title in the $rules array, which should have been name...

这篇关于Laravel验证不会返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆