I want the user to be able to update his own password. i get three inputs while in database i have the password field together with the username and email. I don't really know the flow of controller hope u can help me out.
my controller:
public function changepass() {
$user = Auth::user();
$rules = array(
'old_password' => 'required|alphaNum|between:4,16',
'password' => 'required|alphaNum|between:4,16|confirmed'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::action('HomeController#getpass', $user->id)->withErrors($validator);
} else {
if (!Hash::check(Input::get('old_password'), $user->password)) {
return Redirect::action('HomeController#getpass', $user->id)->withErrors('Old password does not match!');
} else {
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::action('HomeController#getpass', $user->id)->withMessage("Password has been changed successfully!");
}
}
My view:
#extends('master')
#section('content')
<div class="span8 well" style="opacity: 0.9">
<h4>Change password: </h4>
{{ Form::open(array('url' => 'changepass/', 'method'=>'POST')) }}
{{Form::hidden('id',Auth::user()->id)}}
<div class="form-group">
{{ Form::password('old_password', array('class'=>'form-control', 'placeholder'=>'Old Password')) }}<br>
{{ Form::password('new_password', array('class'=>'form-control', 'placeholder'=>'New Password')) }}<br>
{{ Form::password('confirm_new_password', array('class'=>'form-control', 'placeholder'=>'Confirm New Password')) }}
</div>
{{ Form::submit('Update', array('class' => 'btn btn-success')) }}
</div>
#stop
//Route
Route::post('changepass', 'HomeController#changepass');
Related
I'm trying to build contact form in Laravel 5.4. I'm almost succeeded but besides actual message, on my mail i'm getting the structure of the page (look screenshot). Can you help me with that?
enter image description here
my View Form:
<div class="row">
{{ Form:: open(array('action' => 'ContactController#getContactUsForm')) }}
<ul class="errors">
#foreach($errors->all('<li>:message</li>') as $message)
{{ $message }}
#endforeach
</ul>
<div class="form-group">
{{ Form:: textarea ('message', '', array('placeholder' => 'Message', 'class' => 'form-control', 'id' => 'message', 'rows' => '7' )) }}
</div>
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form:: close() }}
</div>
</div>
And my Controller:
namespace App\Http\Controllers;
use Input;
use Illuminate\Http\Request;
use Validator;
use Mail;
use Redirect;
class ContactController extends Controller
{
public function getContactUsForm(Request $request){
//Get all the data and store it inside Store Varible
$data = \Input::all();
//$data = $request->message;
//$data = $request->input('message');
//Validation rules
$rules = array (
//'first_name' => 'required', uncomment if you want to grab this field
//'email' => 'required|email', uncomment if you want to grab this field
'message' => 'required|min:5'
);
//Validate data
$validator = Validator::make ($data, $rules);
//If everything is correct than run passes.
if ($validator -> passes()){
Mail::send('support/contact', $data, function($message) use ($data)
{
//$message->from($data['email'] , $data['first_name']); uncomment if using first name and email fields
$message->from('masha#mail.com', 'contact form');
//email 'To' field: cahnge this to emails that you want to be notified.
$message->to('masha#mail.com', 'Masha')->subject('Contact Form');
});
// Redirect to page
return Redirect::route('contact')
->with('message', 'Your message has been sent. Thank You!');
//return View::make('contact');
}else{
//return contact form with errors
return Redirect::route('contact')
->with('error', 'Feedback must contain more than 5 characters. Try Again.');
}
}
}
change this:
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form:: close() }}
</div>
To this:
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
</div>
{{ Form:: close() }}
I think ordering break the structure.
I'm trying to update some data with image. The other data is updated but the image still not updated. here's my code
route
Route::get('film/{idFilm}/edit', array('as' => 'film.edit', 'uses' => 'FilmController#edit'));
Route::post('film/{idFilm}/update', array('as' => 'film.update', 'uses' => 'FilmController#update'));
controller
public function edit($idFilm)
{
$film = Film::findOrFail($idFilm);
$genre = Genre::lists('namaGenre', 'idGenre');
if (is_null($film))
{
return Redirect::to('film');
}
return View::make('pengelolaan.film.editfilm', compact('film','genre'));
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($idFilm)
{
$rules = array(
'judulFilm' => 'required',
'durasi' => 'required|numeric',
'keterangan' => 'required',
'idGenre' => 'required'
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
return Redirect::to('film/' . $idFilm . '/edit')
->withErrors($validation)
->withInput()
->with('message', 'There were validation errors.');
}
else
{
$films = Film::find($idFilm);
$films->judulFilm=Input::get('judulFilm');
$films->durasi=Input::get('durasi');
$films->keterangan= Input::get('keterangan');
$films->idGenre= Input::get('idGenre');
if(Input::hasFile('foto'))
{
$file=Input::file('foto');
$file->move('img',$file->getClientOriginalName());
$filename=$file->getClientOriginalName();
$films->foto = $filename;
$films->save();
}
else
{
$films->save();
}
Session::flash('message', 'Data Berhasil Diubah');
return Redirect::to('film');
}
}
view
{{Form::model($film, array('route'=>array('film.update', $film->idFilm,'files' => TRUE)))}}
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('judulFilm', 'Judul Film') }}
{{ Form::text('judulFilm', Input::old('judulFilm'), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('durasi', 'Durasi Film') }}
{{ Form::text('durasi', Input::old('durasi'), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('keterangan', 'Sinopsis Film') }}
{{ Form::textarea('keterangan', Input::old('keterangan'), array('class' => 'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('idGenre', 'Genre') }}
{{ Form::select('idGenre', $genre,'',array('class'=>'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('foto', 'Poster') }}
{{ Form::file('foto') }}
</div>
</div>
</br>
<div class="form-group">
<div class="col-lg-6">
<a class="btn btn-default " href="{{ url('film') }}">Batal</a>
{{Form::submit('Simpan', array('type'=>'submit', 'class'=>'btn btn-default'))}}
{{Form::close()}}
There's no error so i don't know whats wrong with it.
Can someone please tell me what's wrong? Thanks in advance
There are few possibilities to face this issue :
Check for have enctype="multipart/form-data" attribute
Check your file size. If your file size is too high increase upload_max_filesize
May be permission dined for your tmp folder. Provide permission for your tem folder. ini_get('upload_tmp_dir');
My code used to work properly but now there is an error
(ErrorException (E_NOTICE)Undefined index: password)
I really dont know why, so if you know please help me. I searched google for similar exceptions and try everything I see but still no success ... Here is my code
route.php
Route::resource('login', 'LoginController');
LoginController
public function store(){
/*$credentials = array(
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password')),
);*/
if(Auth::attempt(['active' => 1])) {
// if(Auth::attempt($credentials)){
if(Auth::attempt(Input::only('username', 'password'))){
return Auth::user();
//return Redirect::back()->with('message','You are now logged in!');
//return Redirect::to('/')->with('message','You are now logged in!');
}
else{
//$errors = ['password' => "Username and/or password invalid."];
//return Redirect::back()->withErrors($errors)->withInput(Input::except('password'));
return 'Failed~';
}
}
else{
//$errors = ['password' => "Please check your email to activate your account"];
//return Redirect::back()->withErrors($errors)->withInput(Input::except('password'));
return 'Failed~';
}
}
}
View login/create.blade.php
#section('content')
<div class="col-lg-3"></div>
<div class="form col-lg-6 box">
{{ Form::open(['route' => 'login.store'], array('class' => 'form-horizontal')) }}
{{-- #if ($error = $errors->first('password'))
<div class="alert alert-danger">
{{ $error }}
</div>
#endif--}}
<div class="input-group form-group-md">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
{{ Form::text('username', null,['class' => 'form-control','autofocus'=>'autofocus', 'placeholder' => 'Username']) }}
</div>
<div class="input-group form-group-md">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
{{ Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password')) }}
</div>
<div class="form-group-md">
Forgot your password?
</div>
<div class="form-group-md">
{{ Form::submit('Log In', array('class' => 'login navbar-btn btn-block form-control', 'autofocus'=>'autofocus')) }}
</div>
<div class="question form-group-md">
{{ Form::label('register', 'Do not have an account?! ') }} Register here!
</div>
{{ Form::close() }}
</div>
<div class="col-lg-3"> </div>
#stop
error
Rewrite your code as below.
$username = Input::get('username');
$password = Input::get('password');
// You also may add extra conditions to the authenticating query
if (Auth::attempt(array('username' => $username, 'password' => $password, 'active' => 1)))
{
// The user is active, not suspended, and exists.
}else{
// user not exist or suspended
}
Refer this link also.
The problem is that the validateCredentials method which is belongs to your class provider EloquentUserProvider expects that the $credentials array have array element called password and expects it to be hashed .
as shown here :
/**
* Validate a user against the given credentials.
*
* #param \Illuminate\Contracts\Auth\Authenticatable $user
* #param array $credentials
* #return bool
*/
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
and you in your code are sending wrong array in the first attempt call
if(Auth::attempt(['active' => 1]))
to solve this you may have to rewrite your code some thing like this ,
public function store()
{
$credentials = array(
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password')),
'active' => 1
);
if(Auth::attempt($credentials)) {
return Auth::user();
} else {
return 'Failed~';
}
}
When a user is created a random password (and a auth code) will be created and send with an email to the user.
When clicked on the link in the email the user status will be 1 (so active) and the user will be able to change his password right away.
Now it doesn't work as I want to.
UserController:
public function store(CreateUserRequest $request, User $user, Attribute $attribute)
// some unnecessary code
if ((Input::get('usertype_id')) > 1) {
$randomPassword = str_random(8);
$user->password = Hash::make($randomPassword);
$authentication_code = str_random(12);
$user->authentication_code = $authentication_code;
$user->active = 0;
};
$user->save();
if ((Input::get('usertype_id')) > 1) {
// Email sturen met verficatie code
$email = Input::get('email');
Mail::send('emails.user', ['user' => $user, 'password' => $randomPassword, 'authentication_code' => $authentication_code], function ($message) use ($email) {
$message->to($email, 'Lilopel')->subject('Lilopel: Verify your account!');
});
};
public function confirmUser($authentication_code)
{
if (!$authentication_code)
{
return 'auth code not found!';
}
$user = User::where('authentication_code', '=', $authentication_code)->first();
if (!$user)
{
return 'user not found!';
}
$user->active = 1;
$user->save();
Session::put('user_id', $user->id);
return view('user.setpassword', ['user' => $user]);
//return redirect()->route('user.setPassword', [$user_id]);
}
public function setPassword(SetPasswordRequest $request)
{
$user_id = Session::get('user_id');
$user = $this->user->find($user_id);
$user->fill($request->only('password'));
$user->save();
}
Route:
Route::get('user/verify/{authenticationCode}', 'UserController#confirmUser');
Route::get('user/password', 'UserController#setPassword');
View:
{!! Form::model($user, ["route"=>['user.setPassword', $user->id] , "method" => 'PATCH']) !!}
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
{!! Form::label('password', trans('common.password'), ['class' => 'form-label col-sm-3 control-label
text-capitalize']) !!}
<div class="col-sm-6">
{!! Form::password('password', ['name' => 'password', "class"=>"form-control","placeholder" =>
trans('common.password') ]) !!}
{!! $errors->first('password', '<span class="help-block">:message</span>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
{!! Form::label('password_confirmation', trans('common.confirmpassword'), ['class' => 'form-label
col-sm-3 control-label text-capitalize']) !!}
<div class="col-sm-6">
{!! Form::password('password_confirmation', ['name' => 'password_confirmation',
"class"=>"form-control","placeholder" => trans('common.confirmpassword') ]) !!}
{!! $errors->first('password_confirmation', '<span class="help-block">:message</span>') !!}
</div>
</div>
{!! Form::submit( trans('common.edit'), ["class"=>"btn btn-primary text-capitalize center-block "]) !!}
{!! Form::close() !!}
Email links works, the status of the user gets active, but then the .blade will give a Route [user.setPassword] not defined. (View: public_html/server2/resources/views/user/setpassword.blade.php) error.
work togetherTo use the route as you do, you need a named route.
Change this
Route::get('user/password', 'UserController#setPassword');
to this
Route::get('user/password', [
'as' => 'user.setPassword',
'uses' => 'UserController#showProfile'
]);
Also, make sure the HTTP verbs of the route and your form's method work together.
sorry if this is a very newbie Q..
but please help me to solve this problem. plus give me the reason about why this error happened..
this is my edit view
new.blade.php
#section('content')
#include('common.show_error')
{{Form::open(array('url'=>'author/update', 'method'=>'PUT'))}}
<p>
{{ Form::label('name', 'Name: ') }}</br>
{{ Form::text('name', $author->name) }}
</p>
<p>
{{ Form::label('bio', 'Biography: ') }}</br>
{{ Form::textarea('bio', $author->bio) }}
</p>
{{ Form::hidden('id', $author->id) }}
<p>{{ Form::submit('Edit Data') }}</p>
#stop
this is my show view
show.blade.php
#extends('layouts.default')
#section('content')
<h1>{{ $author->name }}</h1>
<p>{{ $author->bio }}</p>
<p>{{ $author->updated_at }}</p>
<span>
{{ HTML::linkRoute('authors', 'Home') }} |
{{ HTML::linkRoute('edit_author', 'Edit', array($author->id)) }} |
{{ Form::open(array('url'=>'author/destroy', 'method'=>'DELETE', 'style'=>'display: inline;')) }}
{{ Form::hidden('id', $author->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
</span>
#stop
this is my controller
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
Member::where('id','=',$id)->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated');
}
}
the case: when I try to edit data using edit button. it said:
"Trying to get property of non-object laravel"
and when I check at the error log. it refers to
<h1>{{ $author->name }}</h1>
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
$author = Member::find($id);
$author->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated')
->with('author', $author);
}
}
Little changes in your controller, try it :) In your code, you are not send variable "author" into your view.