Laravel表单在提交之前不显示需要填写的字段

时间:2017-02-15 02:06:12

标签: php laravel-5

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,但规则似乎被忽略,因为仍然可以提交没有输入的表单。

我希望当用户尝试提交表单并且字段未完全填写时,表单会发出警告。

1 个答案:

答案 0 :(得分:0)

规则被忽略了,因为您正在编写

的$ rules和$ files变量
$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
}