JackD's avatar

form inside modal problem

i have this upload picture form inside the modal now before i placed it into a single page and it works properly and restrict the user to upload without a file to upload.

now when i place my form upload code inside a modal it still display the error message for validation but it continue to save without a file to upload, how can i restrict that now?

any help will be a big learning for me :)

0 likes
9 replies
JackD's avatar

controller

public function uploadProfilePicture(Request $request) { $photos = Photo::where('user_id', '=', Auth::id())->get(); if (!$photos->isEmpty()) { foreach ($photos as $photo) { $photo->delete(); } } $this->validate($request, ['photoFilename' => 'required']); if (Input::hasFile('photoFilename')) {

        $photo = Photo::create($request->all());
        $request = $request->all();
        $request['user_id'] = Auth::id();
        $file = Input::file('photoFilename');
        $photoname = md5($file . microtime());
        // $file->move(public_path() . '/hphotos/xpf1/' . $request['user_id'] ."/", uniqid(). '.' .$file->guessClientExtension());
        $file->move(public_path() . '/hphotos/xpf1/' . $request['user_id'] . "/", md5($file . microtime()) . '.' . $file->guessClientExtension());
        $photo->profileimage = $photoname . '.' . $file->guessClientExtension();
        $photo->user_id = $request['user_id'];
        //
        $photo->save();
    }
    return redirect('/');
}

view

<a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a> <div class="modal-dialog"> <div class="modal-header"> × `<h4 class="modal-title">Modal title</h4> `<div class="modal-body">

            {!! Form::open(array('url'=>'form-submit','files'=>true)) !!}

           {!! Form::label('photoFilename','File',array('id'=>'','class'=>'')) !!}
           {!! Form::file('photoFilename','',array('id'=>'','class'=>'')) !!}
            <br/>
            {!! Form::submit('Save') !!}
            {!! Form::reset('Reset') !!}
            {!! Form::close() !!}

        </div>
        <div class="modal-footer">
        </div>
   </div>
</div>
bobbybouwmann's avatar

@dinis you really want us to build your application huh..

Please format your code properly, this is kinda unreadable...

1 like
JackD's avatar

@blackbird no im just trying to learn from experience that's the way how i learn things, by doing and seeing examples

bobbybouwmann's avatar

I think you are going wrong here

$photoname = md5($file . microtime());

$file->move(public_path() . '/hphotos/xpf1/' . $request['user_id'] . "/", md5($file . microtime()) . '.' . $file>guessClientExtension());

$photo->profileimage = $photoname . '.' . $file->guessClientExtension();

You use the md5 method twice with microtime() so there is a change that the output of md5 is different. You need to use the $photoname variable everywhere instead of once

$photoname = md5($file . microtime());

$file->move(public_path() . '/hphotos/xpf1/' . $request['user_id'] . "/", $photoname . '.' . $file>guessClientExtension());

$photo->profileimage = $photoname . '.' . $file->guessClientExtension();
JackD's avatar

what do you mean by "so there is a change that the output of md5 is different"

does it mean that im getting the generated correct filename now and there is a chance that i might get a different filename?

bobbybouwmann's avatar

I mean that if you do md5 twice with a time in it you might get different outputs in the proces because it's based on time. You only do it once and you use that as the filename

JackD's avatar

is there any available open source package for private messaging except for pusher that you can recommend?

Please or to participate in this conversation.