我已经实施了上传员工照片的选项。这是可选的。但是,如果我尝试上传一个无效的文件,它似乎只是跳到show-method,它首先不存在,其次从不调用。
如果我在没有文件的情况下编辑表单,一切正常,验证也可以。如果我上传了一个有效的文件,它也能正常工作。
我只是不知道为什么会发生这种情况
控制器:
`public function update($id){
// get the POST data
$new = Input::all();
// get the user
$u = User::find($id);
$picturevalid = true;
if (Input::get('slett') === 'slett') {
$del = $u->employeePicture;
$del->forceDelete();
}
if(Input::hasFile('picture')) {
if ($u->employeePicture) {
$e = $u->employeePicture;
}
else { $e = new Employeepicture(); }
$image = array(Input::file('picture'));
$picturevalid= $e->validate($image);
}
$new['password'] = $u->password;
// attempt validation
if ($u->validate($new, $u->uid) && $picturevalid && (Input::get('groups') || $new['usertype'] == 2))
{
if (Input::hasFile('picture')) {
$imagein = Input::file('picture');
$kid = Session::get('kid');
$uid = $u->uid;
// We should make an url-friendly version of the file
$newname = $uid.'_'.$u->lastname.'_'.$u->firstname.'.'. // newname:kindergarden+kidnr
$imagein->getClientOriginalExtension(); // punktum + filendelse
$thumb_w = 250; // thumbnail size (area cropped in middle of image)
$thumb_h = 300; // thumbnail size (area cropped in middle of image)
$this->resize_then_crop($imagein,$newname,$thumb_w,$thumb_h,/*rgb*/"255","255","255");
//url to be stored in the database
$urlpath = '/Hovedprosjekt/app/storage/upload/'.$kid.'/images/thumbs/';
$url = $urlpath.$newname;
$e->uid = $uid;
$e->url = $url;
$e->updated_at = date('Y-m-d H:i:s');
if(!$e->save())
{
//save unsuccessful
return Redirect::to('employee/'.$id.'/edit')
->with('register_message', 'Endring feilet.');
}
}
$u->firstname = $new['firstname'];
$u->lastname = $new['lastname'];
$u->phone = $new['phone'];
$u->address = $new['address'];
$u->email = $new['email'];
$u->zipcode = $new['zipcode'];
$u->updated_at = date('Y-m-d H:i:s');
$usertype = Usertype::find($new['utid']);
$u = $usertype->user()->save($u);
//saves user to db
if($u->save())
{
if($new['utid'] == 2){
//Styrer
$kid = Session::get('kid');
$kindergarden = Kindergarden::find($kid);
$u->group()->sync(array($kindergarden->leaderGroup()->gid));
}
else {
$groups = Input::get('groups');
if(is_array($groups))
{
$u->group()->sync($groups);
}
}
return Redirect::to('employee')
->with('message', 'Ansatt endret!');
}
else
{
//save unsuccessful
return Redirect::to('employee/' . $id . '/edit')
->with('register_message', 'Endring feilet.');
}
}
else
{
//validation unsuccessful
//Get validadtion errors
$errors = $u->errors();
if(Input::hasFile('picture')) {
$perrors = $e->errors();
if(isset($perrors)){
foreach($perrors->all() as $perror){
$errors->add($perror);
}
}
}
return Redirect::to('employee/'.$id.'/edit')
->withErrors($errors) // send back all errors to the login form
->withInput(Input::all()); // send back the input (not the password) so that we can repopulate the form
}
}`
型号: Employeepicture.php
`private $rules = array(
'picture' => 'mimes:jpg,png,gif'
);
private $errors;
/**
* Validation method
*
* @return boolean
*/
public function validate($data) {
// make a new validator object
$v = Validator::make($data, $this->rules);
// check for failure
if ($v->fails()) { // set errors and return false
$this->errors = $v->errors();
return false;
}
// validation pass
return true;
}`
user.php的
`private $rules = array(
'firstname' => 'required|max:40',
'lastname' => 'required|max:40',
'email' => 'required|email|unique:user', // make sure the email is an actual email
'phone' => 'required|numeric|digits_between:7,20',
'address' => 'required|min:3',
'picture' => 'mimes:jpg,jpeg,png,gif|max:2046',
'zipcode' => 'required|numeric|digits_between:1,4'
//'password' => 'required|min:3',
//'password_confirmation' => 'same:password'//required?
);
/**
* Valditation errors
*/
private $errors;
/**
* Validation method
*
* @return boolean
*/
public function validate($data, $update)
{
if (isset($update)) {
$this->rules['email'] = 'required|email|unique:user,email,' . $update . ',uid';
}
// make a new validator object
$v = Validator::make($data, $this->rules);
// check for failure
if ($v->fails())
{
// set errors and return false
$this->errors = $v->errors();
return false;
}
// validation pass
return true;
}
/**
* Validation errors
*/
public function errors()
{
return $this->errors;
}`
编辑视图:
`@extends('employee.employeelayout')
@section('title', 'Endre ansatt')
@section('employeecontent')
<h3>Endre ansatt</h3>
@if (Session::has('register_message'))
<div id="register_message"><p>{{ Session::get('register_message') }}</p></div>
@endif
<ul>
@foreach($errors->all() as $error)
<li>{{ str_replace($eng, $nor, $error) }}</li>
@endforeach
</ul>
{{ Form::model($employee, array('route' => array('employee.update', $employee->uid), 'method' => 'PUT', 'files'=>true)) }}
<div class="form-group">
{{ Form::label('firstname', 'Fornavn', array('class'=>'control-label')) }}
{{ Form::text('firstname', null, array('class'=>'form-control', 'placeholder'=>'Fornavn')) }}
</div>
<div class="form-group">
{{ Form::label('lastname', 'Etternavn', array('class'=>'control-label')) }}
{{ Form::text('lastname', null, array('class'=>'form-control', 'placeholder'=>'Etternavn')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'E-post', array('class'=>'control-label')) }}
{{ Form::text('email', null, array('class'=>'form-control', 'placeholder'=>'E-post')) }}
</div>
<div class="form-group">
{{ Form::label('phone', 'Telefonnummer', array('class'=>'control-label')) }}
{{ Form::text('phone', null, array('class'=>'form-control', 'placeholder'=>'Telefonnummer')) }}
</div>
<div class="form-group">
{{ Form::label('address', 'Adresse', array('class'=>'control-label')) }}
{{ Form::text('address', null, array('class'=>'form-control', 'placeholder'=>'Adresse')) }}
</div>
<div class="form-group">
{{ Form::text('zipcode', null, array('class'=>'form-control', 'placeholder'=>'Postnummer')) }}
</div>
<div class="form-group">
{{ Form::label('image', 'Bilde av ansatt(valgfritt): ') }}
{{ Form::file('picture', null, array('class'=>'input-block-level', 'placeholder'=>'Picture')) }}
@if($employee->employeePicture)
<h5>Nåværende bilde</h5>
<img class="small-pic" src="{{$employee->employeePicture->url}}"/><br>
{{ Form::checkbox('slett', 'slett'); }} Fjern
@endif
</div>
<div class="form-group">
{{ Form::label('utid', 'Brukertype', array('class'=>'control-label')) }}
{{ Form::select('utid', $usertypes, $employee->utid, array('class'=>'form-control','id'=>"select_usertype")) }}
</div>
<div class="form-group" id="group">
{{ Form::label('group', 'Avdeling', array('class'=>'control-label')) }}
@foreach($groups as $group)
@if ($employee->group->contains($group->gid))
<div class='checkbox'>{{ Form::checkbox('groups[]', $group->gid, true) }} {{ $group->name }}</div>
@else
<div class='checkbox'>{{ Form::checkbox('groups[]', $group->gid) }} {{ $group->name }}</div>
@endif
@endforeach
</div>
<div class="pull-right">
{{ Form::submit('Endre', array('class'=>'btn btn-primary'))}}
</div>
{{ Form::close() }}
<a class="btn btn-primary" href="{{ URL::to('employee') }}"><span class="glyphicon glyphicon-arrow-left"></span> Tilbake</a>`