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.
Related
So I have this Editform in my office page the problem is when I press the edit button it says this
Missing required parameters for [Route: editoffice] [URI: building/{id}/offices/{office_id}/edit]. (View: C:\xampp\htdocs\Eguide\resources\views\editoffice.blade.php)
Routes
Route::get('building/{id}/offices/{office_id}/edit', 'OfficeController#edit')->name('editofficeform');
Route::post('building/{id}/offices/{office_id}/edit', 'OfficeController#update')->name('editoffice');
Building.blade.php
This is the code for the edit button
Edit
OfficeController.php
public function edit(Request $request, $id)
{
$office_id = $request->get('office_id');
$office = Office::find($office_id);
return view('editoffice')->withOffice($office)->with('id',$id);
}
public function update(Request $request, $id)
{
$office = Office::find($id);
$office->name =$request->officename;
$office->floor = $request->floor;
$office->update();
\Session::flash('building_flash', 'Updated successfully!');
return redirect()->back();
}
editoffice.blade.php
#extends('layouts.main')
#section('title', 'Create an Office')
#section('content')
{!! Form::open(array('route' => ['editoffice', $id], 'class' => 'form')) !!}
<div class="container">
<div class="form-group">
{!! Form::label('Office Name') !!}
{!! Form::text('officename', $office->name, array('required',
'class'=>'form-control',
'placeholder'=>'Office Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Office Floor') !!}
{!! Form::text('floor', $office->floor, array('required',
'class'=>'form-control',
'placeholder'=>'Office Floor')) !!}
</div>
<div class="form-group">
{!! Form::submit('Update Office',
array('class'=>'btn btn-primary')) !!}
Back
</div>
{!! Form::close() !!}
#endsection
Whats wrong with my code?
Change the form to:
{!! Form::open(array('route' => ['editoffice', [$id, $office->id]], 'class' => 'form')) !!}
Also, change the edit and update methods to:
public function edit($id, $office_id) {
$office = Office::find($office_id);
return view('editoffice', compact('office', 'id'));
}
public function update(Request $request, $id, $office_id)
I'm having a problem for the first time when i submit a form.
When i submit the form it doesn't go to post route and i don't know why.
my post route is that:
Route::post('/app/add-new-db', function()
{
$rules = array(
'name' => 'required|min:4',
'file' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('app/add-new-db')
->withErrors($validator);
}
else
{
$name = Input::get('name');
$fname = pathinfo(Input::file('file')->getClientOriginalName(), PATHINFO_FILENAME);
$fext = Input::file('file')->getClientOriginalExtension();
echo $fname.$fext;
//Input::file('file')->move(base_path() . '/public/dbs/', $fname.$fext);
/*
DB::connection('mysql')->insert('insert into databases (name, logotipo) values (?, ?)',
[$name, $fname.$fext]);
*/
//return Redirect::to('app/settings/');
}
});
And my html:
<div class="mdl-cell mdl-cell--12-col content">
{!! Form::open(['url'=>'app/add-new-db', 'files'=>true]) !!}
<div class="mdl-grid no-verticall">
<div class="mdl-cell mdl-cell--12-col">
<h4>Adicionar Base de Dados</h4>
<div class="divider"></div>
</div>
<div class="mdl-cell mdl-cell--6-col">
<div class="form-group">
{!! Form::label('name', 'Nome: ') !!}
{!! Form::text('name', null, ['id'=> 'name', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Logotipo:') !!}
{!! Form::file('image', ['class'=>'form-control']) !!}
#if ($errors->has('image'))
<span class="error">{{ $errors->first('image') }}</span>
#endIf
</div>
<div class="form-group">
{!! Form::submit('Adicionar', ['id'=>'add-new-db', 'class'=>'btn btn-default']) !!}
<p class="text-success"><?php echo Session::get('success'); ?></p>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
I'm loading this from from a jquery get:
function addDB()
{
$( "#settings-new-db" ).click(function(e)
{
$.get('/app/add-new-db', function(response)
{
$('.content-settings').html("");
$('.content-settings').html(response);
componentHandler.upgradeDom();
});
e.preventDefault();
});
}
When i try to submit the form i'm getting 302 found in network console from chrome.
I'm doing forms at this way and it is happens for the first time. Anyone can help me?
Thanks
Fix the name attribute for your image upload field. You refer it as image in the form but you are trying to fetch it as file in your controller.
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');
EDIT
This issue has been solved, but I don't understand why the answer worked.
I would like to know why it didnt work.
Is there some one who could explain me?
Original question
I am stuck on my settings form, my problem is that at the settings form you can enter some email settings but you can also change your password.
The email settings and password reset works and my form fills it self with the data of the current user. But when the form validation fails it redirecteds me back to the form without the form data.
I am not sure if I made myself clear, but this code below will explain it.
ChangeUserSettingRequest.php
class ChangeUserSettingsRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
if (\Auth::check()) {
return true;
}
return false;
}
/**
* Get the validation rules that apply to the request.
*
* WHEN THIS VALIDATION FAILS IT GOES BACK TO SETTINGS.BLADE.PHP
* BUT IT DOES NOT KEEP THE SETTINGS DATA IN THE FORM
*/
public function rules()
{
return [
'current_password' => 'sometimes|required_with:password',
'password' => 'required_with:current_password|min:8|confirmed',
'password_confirmation' => 'required_with:password',
];
}
}
settings.blade.php
{!! Form::model($settings, array('url' => route('store.settings'), 'class' => 'col s12')) !!}
<p>#lang('forms.info.settings')</p>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('info_mail', 0, false, ['id' => 'info_mail']) !!}
{!! Form::label('info_mail', Lang::get('forms.newsletter'), ['for' => 'info_mail']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('message_notification', 0, false, ['id' => 'message_notification']) !!}
{!! Form::label('message_notification', Lang::get('forms.messages'), ['for' => 'message_notification']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('friend_notification', 0, false, ['id' => 'friend_notification']) !!}
{!! Form::label('friend_notification', Lang::get('forms.friendrequest'), ['for' => 'friend_notification']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('item_notification', 0, false, ['id' => 'item_notification']) !!}
{!! Form::label('item_notification', Lang::get('forms.reactiononitem'), ['for' => 'item_notification']) !!}
</div>
#if ($settings and $settings->google_maps !== null)
<div class="settings-explain">
<p class="margin-top-20">#lang('forms.info.companysettings')</p>
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('type', 0, false, ['id' => 'type']) !!}
{!! Form::label('type', Lang::get('forms.companytype'), ['for' => 'type']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('google_maps', 0, false, ['id' => 'google_maps']) !!}
{!! Form::label('google_maps', Lang::get('forms.companymap'), ['for' => 'google_maps']) !!}
</div>
#endif
<div class="settings-explain">
<p class="margin-top-20">#lang('forms.info.changepassword')</p>
</div>
<div class="input-field col s12">
{!! Form::label('current_password', $errors->has('current_password') ? $errors->first('current_password') : Lang::get('forms.currentpassword'), ['for' => 'current_password']) !!}
{!! Form::password('current_password', ['class' => $errors->has('current_password') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::label('password', $errors->has('password') ? $errors->first('password') : Lang::get('forms.newpassword'), ['for' => 'password']) !!}
{!! Form::password('password', ['class' => $errors->has('password') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::label('password_confirmation', $errors->has('password_confirmation') ? $errors->first('password_confirmation') : Lang::get('forms.repeatpassword'), ['for' => 'password_confirmation']) !!}
{!! Form::password('password_confirmation', ['class' => $errors->has('password_confirmation') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::button(Lang::get('forms.save'), ['class' => 'btn waves-effect waves-light', 'type' => 'submit', 'name' => 'Save']) !!}
</div>
{!! Form::close() !!}
UserInfoController.php
/**
* Function shows the settings form
*/
public function showSettings()
{
$title = Lang::get('titles.settings');
$user_id = Auth::User()->id;
$settings = $this->settings->getUserSettings($user_id);
$companySettings = $this->companySettings->getSettings($user_id);
if ($companySettings) {
$settings->type = $companySettings->type;
$settings->google_maps = $companySettings->google_maps;
}
return view('pages.users.settings', ['title' => $title])->with(compact('settings'));
}
/**
* Function stores the setting changes
*
* ChangeUserSettingsRequest makes sure that the request is valid
*/
public function storeSettings(ChangeUserSettingsRequest $request)
{
$id = Auth::User()->id;
$success = $this->settings->handleStoreSettingsRequest($request);
// Checks if user has company settings
$hasCompanySettings = $this->companySettings->checkForSettings($id);
// If user has company settings
if ($hasCompanySettings === true) {
// Update company settings
$this->companySettings->updateSettings($request);
}
if ($success === true) {
/* Get translated message */
$message = Lang::get('toast.settingsstored');
return Redirect::route('user.profile', array(Auth::User()->permalink))->withMessage($message);
}
$settings = $this->settings->getUserSettings($id);
/* Get translated message */
$message = Lang::get('forms.error.wrongpassword');
/* This works and the form is filled with the correct data after it redirects me back */
return Redirect::back()->withErrors(array('current_password' => $message))->withSettings($settings);
}
Request.php
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
So my problem is that in my UserInfoController I redirect back and it has the good data but when my ChangeUserSettingsRequest redirects me back the form is empty.
Does anyone know why ChangeUserSettingsRequest does not send the data back?
I am not talking about the password data but about the email settings.
I do not want to make a costum validator as I think this should be posible.
Please note: when the validation fails, the function storeSettings will not be executed at all
On failure return them back to the page with their input data (withInput())
return Redirect::back()->withErrors(['current_password' => $message])->withInput($request->except('password'));
For more information see the Laravel docs pertaining to requests... http://laravel.com/docs/5.1/requests
I found it!
The formRequest should have handled it correctly but no idea why it did not.
I found the solution when looking trough the file formRequest.php
https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Http/FormRequest.php
I had to override the response function and it works now!
To only thing that I had to add to my ChangeUserSettingsRequest was this
public function response(array $errors)
{
return $this->redirector->to($this->getRedirectUrl())->withErrors($errors, $this->errorBag);
}
The only difference is that I don't send the input fields back, little strange that this works but I am glad that it is solved now.
I am in the progress of making a form to edit an existing entry in the database. I am using the Form::model approach to do this, however it doesn't seem to work. The fields just stay empty.
ServerController.php
/**
* Editing servers
*/
public function edit($name)
{
$server = Server::find($name);
$keywords = ($server->getKeywords()) ? $server->getKeywords() : array();
$countries = $this->getCountries();
return View::make('server/edit', array('server' => $server, 'countries' => $countries));
}
public function update($name)
{
$server = Server::find($name);
// Did it succeed?
if($server->save()) {
Session::flash('success', 'You server was edited!');
return Redirect::route('server.view', array($name));
}
// Did not validate
if(Input::get('keywords')) {
$keywords = Input::get('keywords');
Session::flash('keywords', $keywords);
}
Session::flash('danger', "<b>Oops! There were some problems processing your update</b><br/>" . implode("<br/>", $server->errors()->all()));
return Redirect::route('server.edit', array($name))->withInput()->withErrors($server->errors());
}
The Form
{{ Form::model($server, array('route' => array('server.update', $server->name), 'class' => 'form-horizontal', 'role' => 'form', 'files' => true)) }}
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{{ Form::label('email', 'Email', array('class' => 'control-label col-md-4')) }}
<div class="col-md-4">
{{ Form::text('email', '', array('class' => 'form-control')) }}
{{ $errors->first('email', '<br/><div class="alert alert-danger">:message</div>') }}
</div>
</div>
(some more fields)
{{ Form::close() }}
The problem here is that you're passing in an empty string as the default field value. As the documentation states here, any explicitly passed values will overrule the model attribute data. Try using null instead of '':
{{ Form::text('email', null, array('class' => 'form-control')) }}