且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel验证不会返回错误

更新时间:2022-10-27 19:25:27

几乎总是出现批量分配错误的原因是 $ 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...