mardi 14 février 2017

Laravel form not showing fields required to be filled in before submitting

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://ift.tt/19U9XpV
  $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');

}

The form submits and stores the data to sql, but the rules seems to be ignored as form with no inputs can still be submitted.

I am expecting the form to gives out alert when users try submitting form with the fields are not fully filled in.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lIQyof
via IFTTT

Aucun commentaire:

Enregistrer un commentaire