I'm in trouble with Laravel framework. I have two forms, but they do the same thing: signup.
Here's a deal. I have a signup page. I also have a signup form on my home page.
When I fill form on my home page and click "Sign up", it's validate my form. I don't want it. I want it to redirect POST to /signup and then validate. I want to display error/success info on /signup page. How can I do this?
my form on home.blade.php (signup.blade.php has the same form):
<form action="{{ route(auth.signup) }}" method="POST">
<input name="username" type="text" placeholder="Username">
<input name="email" type="email" placeholder="Email">
<input name="password" type="password" placeholder="Password">
<input name="password_confirmation" type="password" placeholder="Confirm Password">
<input type="submit" value="Sign up">
Is not what you are asking but I would do an "ajax registration" in your home page and show the proper validation error in your home page. If you need to have both singups (home, fullpage) you can differentiate your post request doing this on your auth.singup:
if ( $request->ajax() ){
return response()->json([
'auth' => $auth, // Your validation result
'intended' => URL::previous()
]);
}
Related
I have two forms on a page. The first is simply to allow the user to access blocked content. (I understand the risks associated with going this route, but it's just to allow members to view certain information that does not pose any sort of risks).
This is at the top of the page...
<?php
if (isset($_POST['password']) && $_POST['password'] == 'password') {
setcookie("password", 'password', strtotime('+1 day'));
header('Location: index.php');
exit;
}
?>
.....
<form method="POST">
<input type="text" name="password">
<input type="submit" class="btn btn-full" value="Continue" />
</form>
I believe this is preventing the second form (below) from operating (when you click the submit button, nothing happens. It's as if you hadn't hit the button at all).
<form action="handler.php" method="post" class="contactForm">
<div class="form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
</div>
<div class="text-center">
<input type="submit" value="Send Message" class="btn btn-full" />
</div>
</form>
What can I do to fix the first part to allow the second form to function?
Well, I tried it and it worked.
1) Make sure you have the handler.php file at the directory of index.php(according to your code).
2) Check for any console errors. If there is any then please attach it to your post.
3) and did you override the form submit event or input button press event from js?
Also I don't see anything where you are restricting the second form to appear when password is not set in cookie..
so better use:
<?php
if (isset($_COOKIE['password'])): ?>
YOUR SECOND FORM
<?php endif; ?>
and hide the first form when password is entered.
I'm trying to code a login system in Laravel and it keeps telling me the text box is required. I've added the textbox, rechecked the names are correct and I've made sure I enter text on submit of the form it belows to.
It keeps saying "The credentials.username field is required.", if I remove the required from the validation it will say it for password too.
HTML:
<form method="post">
<div id="login-columns">
<div id="login-column-1">
<label for="credentials-email">Username</label>
<input id="credentials-email" name="credentials.username" tabindex="2" type="text">
<input id="credentials-remember-me" name="_login_remember_me" tabindex="5" type="checkbox">
<label class="sub-label" for="credentials-remember-me">Keep me logged in</label>
</div>
<div id="login-column-2">
<label for="credentials-password">Password</label>
<input id="credentials-password" name="credentials.password" tabindex="3" type="password">
</div>
<input name="_token" type="hidden" value="{{ csrf_token() }}">
<div id="login-column-3">
<input style="margin: -10000px; position: absolute;" type="submit" value="Login"> <a class="button" href="#" id="credentials-submit" tabindex="4"><b></b><span>Login</span></a>
</div>
<div id="login-column-4">
888 Online
</div>
</div>
</form>
PHP:
public function onPost(Request $request)
{
$validator = Validator::make($request->all(), [
'credentials.username' => 'required|exists:users',
'credentials.password' => 'required'
]);
if ( $validator->fails()) {
return Redirect::back()->withErrors($validator->messages());
}
else {
if (!Auth::attempt(['username' => $request->input('credentials.username'), 'password' => $request->input('credentials-password')])) {
return Redirect::back()->withMessage('Failed Authentication')->withColor('danger');
}
else {
$user = Auth::user();
$user->save();
return Redirect::to('/home');
}
}
}
I believe the issue you are seeing is that Laravel treats 'credentials.username' as a "Nested Attribute". For instance if you are rendering a view 'layout.head' it will automatically look for the file head.blade.php inside the layout folder.
I think in this case it's assuming you are passing it an array like:
<input id="credentials-email" name="credentials[username]" tabindex="2" type="text">
<input id="credentials-password" name="credentials[password]" tabindex="3" type="password">
Have you tried:
<input id="credentials-email" name="credentials_username" tabindex="2" type="text">
<input id="credentials-password" name="credentials_password" tabindex="3" type="password">
There is a very brief mention of nested attributes on the Laravel validation documentation page: https://laravel.com/docs/5.4/validation
Don't use dot inside name input.
Replace credentials.username by credentials_username.
Or even better, just username.
In laravel validation dot(.) Represent array of items. In this case you need to modify your input field name like this <input name="credentials[username]"> <input name="credentials[password]">
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.
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'm currently creating a simple jQuery Mobile sign in page. Here's my code:
<div data-role="page">
<div data-role="header"><h1>Header</h1></div>
<?php
if(isset($_POST['username']) && isset($_POST['password'])){
die("hello");
}
?>
<div data-role="content">
<form method="post">
<div data-role="fieldcontain">
<label for="username">Username:</label>
<input id="username" type="text" name="username" />
</div>
<div data-role="fieldcontain">
<label for="password">Password:</label>
<input id="password" type="password" name="password" />
</div>
<input type="submit" value="Sign In" data-inline="true" data-theme="b" />
</form>
</div>
<div data-role="footer"><h4>Footer</h4></div>
</div>
Whenever I type in a username and password, using firebug, I can tell that the correct post was sent, however, the page that is returned through ajax to load into the page does not have the die("hello") in it. It is just the same page that I entered the login information on.
Am I using jQuery Mobile's form system wrong?
You may want to consider turning off Ajax, while you are troubleshooting your form submission. The Ajax handling of form submissions inside JQuery Mobile is implemented automatically to create a smooth transition between the form and the result. Details, here. To submit a form without Ajax, you can either disable Ajax form handling globally, or per form via the
data-ajax="false"
attribute. Or, globally,
$(document).bind("mobileinit", function () {
$.mobile.ajaxEnabled = false;
});