My form submission controller
// Code to get input from user and validation is done
// if validation fails
return Redirect::to(URL::route('showlogin'))->withInput();
showlogin method
$view->email = Input::flashOnly('email');
$view->password = Input::flashOnly('password');
return $view;
view page
<input type="text" value="{{ $email }}" name="email">
<input type="password" value="{{ $password }}" name="password">
The page does not fill the box with previous input when the validation fails. Have I missed something here?
Use Laravel form helper to create form inputs to remember old input
<input type="text" value="{{ $email }}" name="email">
<input type="password" value="{{ $password }}" name="password">
will be
{{Form::email('email', Input::old('email') ) }}
{{Form::text('password', Input::old('password')) }}
Related
I am working on laravel 5.3 and i want to get authenticated user email as my field value my field code is
<div class="col-md-8">
<input type="text" class="form-control" name="email" value="{{ isset($student->email) ? $student->email : '{{Auth::user()->email}}' }}" required />
</div>
Here above i tried as {{Auth::user()->id}}' }} but its give syntax error if
syntax error, unexpected '}'
If i try with single bracket it prints as it is
You have syntax error,
<input type="text" class="form-control" name="email" value="{{ isset($student->email) ? $student->email : Auth::user()->email }}" required />
Copy paste this code,
you tried to give {{}} inside {{}} to fetch auth details.
Give it a try, it should work.
instead of
<input type="text" class="form-control" name="email" value="{{ isset($student->email) ? $student->email : Auth::user()->email }}" required />
Try,
#php($email=Auth::user()->email)
#if(isset($student->email))
$email=$student->email
#endif
<input type="text" class="form-control" name="email" value="{{ $email }}" required />
I'm new to Laravel and I making some test on a system which use version 4.2. I'm trying to follow Documentation for password reset. So far I'm able to post my email for password reset and I get token on my email.
When I open the URL from email with the token I get this error:
exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException'
The url is: http://example.com/reset/20e2535a11f7c88d1132c55c752a3a8569adbf5f
This is my route
Route::get('/password', ['uses' => 'RemindersController#getRemind']);
Route::get('/reset', ['uses' => 'RemindersController#getReset']);
This is in RemindersController
public function getReset($token = null)
{
if (is_null($token)) App::abort(404);
return View::make('site.reset')->with('token', $token);
}
And the form from the doc's
<form action="{{ action('RemindersController#postReset') }}" method="POST">
<input type="hidden" name="token" value="{{ $token }}">
<input type="email" name="email">
<input type="password" name="password">
<input type="password" name="password_confirmation">
<input type="submit" value="Reset Password">
</form>
I understand the error.. it is saying that the path/file isn't found but it is there..
in your html form, there is the action() method called RemindersController#postReset:
<form action="{{ action('RemindersController#postReset') }}" method="POST">
<input type="hidden" name="token" value="{{ $token }}">
<input type="email" name="email">
<input type="password" name="password">
<input type="password" name="password_confirmation">
<input type="submit" value="Reset Password">
</form>
but your route uses GET. You have to use POST
change your route from:
Route::get('/reset', ['uses' => 'RemindersController#getReset']);
to:
Route::post('/reset', ['uses' => 'RemindersController#getReset']);
i think you could use this way. its maybe better:
Route::match(['GET','POST'], ['uses' => 'RemindersController#getRemind']);
Update: Route should have also token in it because the url is /reset/token:
Route::get('/reset/{token}', ['uses' => 'RemindersController#getReset']);
check if your default controller or default security controller isn't loaded somewhere and it doesn't not overwrite the 'reset' route, get in your application directory using command line and type:
php artisan routes
This should show you if your route is registered and to which controller/action.
I made it, I register the user and when I confirm the user the app makes login automatically.
If I close the session, I can't open a new session with that new user from my custom register form, I got a flag of "bad credentials", but with same login form I can login perfectly with users registered with the FOSUserbundle register form. Both forms have the same code.
<form id="login-form" name="login-form" class="nobottommargin" action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<label for="login-form-username">Correo Electrónico:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" class="form-control" required="required" />
<label for="login-form-password">Contraseña:</label>
<input type="password" id="password" name="_password" required="required" value="" class="form-control" />
<button class="button" id="login-form-submit" name="login-form-submit" value="login">Acceder</button>
</form>
This is the code I'm using in the controller for create and update the info of the user:
$userManager = $this->container->get('fos_user.user_manager');
$em = $this->container->get('doctrine')->getEntityManager();
$userUtils = $this->container->get('fos_user.util.token_generator');
$user = $userManager->createUser();
$user->setNombre($nombre);
$user->setEmail($email);
$user->setUsername($email);
$user->setPlainPassword($pass);
$user->setEnabled(true);
$user->setConfirmationToken($userUtils->generateToken());
$user->addRole('ROLE_USER');
$this->mailer->sendConfirmationEmailMessage($user);
$em->persist($user);
$em->flush();
My routes is here
Route::get('sign-up', ['as' => 'signUp', 'uses' => 'UserController#signUpGet']);
Route::post('sign-up', ['as' => 'signUpPost', 'uses' => 'UserController#signUpPost']);
Controller
return redirect('signUp')->withInput();
And View
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
</form>
The {{old()}} function return empty value.
EDIT
I took
NotFoundHttpException in RouteCollection.php line 145:
Your problem looks like you are not actually submitting the username in the first place:
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
</form>
There is no 'submit' button inside the form. If you submit outside the form - then the username will not be included.
Add the submit button inside your form - then try again
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
<input type="submit" value="Submit">
</form>
Edit - also your controller is wrong. It should be this:
return redirect()->route('signUp')->withInput();
All you are missing is to Flash the Input to the session. This is so it's available during the next request.
$request->flash();
Do that just before calling to View your form.
Source: http://laravel.com/docs/5.1/requests#old-input
<div class="form-group #if($errors->first('username')) has-error #endif">
<label for="username" class="rtl">Enter user name </label>
<input type="text" name="username" class="form-control rtl basic-usage" id="username" placeholder="Enter user name" value="{!! old('username') !!}">
<span class="help-block required">{{$errors->first('username')}}</span>
</div>
above form field will show old value entered.
will show validation error (you have to specify error separately.)
have a place holder.
bootstrap to look better.(add bootstrap)
Your name in the register view should correspond with keys in create() and validate() method inside your RegisterController file.
My problem here was caused by "data-prefill" in the input. Once I removed this, it worked.
You can try this: {{ Input::old('username') }}.
I have this login form via Symfony2 security documentation with the following TWIG template content:
<form action="{{ path('login_check') }}" method="post">
<div class="input form">
<label for="username">Account name or E-mail:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" required="required" />
</div>
<div class="input form">
<label for="password">Password:</label>
<input type="password" id="password" name="_password" required="required" />
</div>
<input type="hidden" name="_token" value="{{ csrf_token("intention") }}">
<button type="submit">Log In</button>
</form>
And I want to add CSRF protection in this form. As you can see, I added this line <input type="hidden" name="_token" value="{{ csrf_token("intention") }}"> but I'm not really sure, if it is enough to activate this protection.
My controller has same form as on the doc, so it looks like this:
<?php
// src/Acme/SecurityBundle/Controller/SecurityController.php;
namespace Acme\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(
SecurityContext::AUTHENTICATION_ERROR
);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render(
'AcmeSecurityBundle:Security:login.html.twig',
array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
)
);
}
}
So it's enough just paste one hidden input with value {{ csrf_token("intention") }} or I have to add something into controller?
I found that Answer from #Chris McKinnel isn't true. Now, the Symfony2 has on the tutorial page this section:
http://symfony.com/doc/current/cookbook/security/csrf_in_login_form.html
I need add line in my security.yml:
form_login:
# ...
csrf_provider: form.csrf_provider
And change this
<input type="hidden" name="_token" value="{{ csrf_token("intention") }}">
to
<input type="hidden" name="_csrf_token" value="{{ csrf_token("authenticate") }}">
Now I am sure my authentication form si CSRF protected.
CSRF protection is enabled by default, so all you need to do is render your CSRF token in your HTML. You don't need to do anything in your controller.
You have a couple of options:
Do it just like you've done it above
Use {{ form_rest(form) }}, this will render all fields that you haven't rendered yet, including hidden fields like the CSRF token
Either will work fine.