2017-02-15 76 views
1
public function edit($id) 
{ 
    $company = \Auth::user()->company; 
return view('user.company.edit') ->with('company', $company); 
} 

/** 
* Update the specified resource in storage. 
* 
* @param \Illuminate\Http\Request $request 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function update(Request $request, $id) 
{ 
    $company = \App\Company::find($id); 
    // validate 
    // read more on validation at http://laravel.com/docs/validation 
    $rules = array(
     'name'  => 'required', 
     'reg'   => 'required', 
     'email'  => 'required|email', 
     'phone'  => 'required|numeric', 
     'address'  => 'required', 
     'desc'  => 'required' 
); 
    $validator = \Validator::make(Input::all(), $rules); 

    // store 
    $company->name = Input::get('name'); 


    // getting all of the post data 
     $file = array('logo' => Input::file('logo')); 
    // setting up rules 
    $rules = array('logo' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000 
    // doing the validation, passing post data, rules and the messages 
    $validator = \Validator::make($file, $rules); 
    if ($validator->fails()) { 
    // send back to the page with the input data and errors 
    return \Redirect::to('/company/'.$company->id.'/edit/'); 
    } 
    else { 
    if ($request->hasFile('logo')){ 
     // checking file is valid. 

     if (Input::file('logo')->isValid()) { 
      \File::delete(public_path() . '/uploads/company_logo/'. $company->logo); 
      $destinationPath = 'uploads/company_logo'; // upload path 
      $extension = Input::file('logo')->getClientOriginalExtension(); // getting image extension 
      $fileName = rand(11111,99999).'.'.$extension; // renameing image 
      Input::file('logo')->move($destinationPath, $fileName); // uploading file to given path 
      $company->logo = $fileName; 
     } 
    } 
    } 

    $company->user_id  = \Auth::user()->id; 
    $company->reg   = Input::get('reg'); 
    $company->email  = Input::get('email'); 
    $company->phone  = Input::get('phone'); 
    $company->address  = Input::get('address'); 
    $company->desc  = Input::get('desc'); 
    //$company->save(); 
    if($company->save()) die('edited'); else die('failed'); 
    // redirect 
    Session::flash('message', 'Successfully edited company!'); 
    return Redirect::to('company/edit'); 

} 

表单提交并将数据存储到sql,但规则似乎被忽略,因为没有输入的表单仍然可以提交。Laravel表单在提交之前没有显示需要填写的字段

我期待的形式给出了警报,当用户尝试提交表单与字段不完全填充。

+0

为什么两个验证规则,只是检查失败的第二次不是第一次? – C2486

回答

0

规则越来越被忽视,因为你是在写$规则和$文件变量由

$rules = array('logo' => 'required'); 
$files = array('logo' => Input::file('logo')); 

你应该做的是修改上面的代码本

$rules['logo'] = "required"; 
$files['logo'] = Input::file('logo'); 

现在,修改控制器的方法是这样的

public function update(Request $request, $id) 
{ 
    $company = \App\Company::find($id); 

    $rules = array(
     'name'  => 'required', 
     'reg'   => 'required', 
     'email'  => 'required|email', 
     'phone'  => 'required|numeric', 
     'address'  => 'required', 
     'desc'  => 'required' 
    ); 

    $files = Input::all(); 

    $files['logo'] = Input::file('logo'); 

    $rules['logo'] = 'required|mimes:jpeg,bmp,png'; 

    // doing the validation, passing post data, rules and the messages 
    $validator = \Validator::make($files, $rules); 
    if ($validator->fails()) { 

     return \Redirect::to('/company/'.$company->id.'/edit/'); 
    } 

    // ... Your Logic 
} 
+0

他也没有发送错误失败后,在窗体中显示。 – C2486

+0

是的,我刚刚更新了答案。 –