Support the ongoing development of Laravel.io →
posted 10 years ago
Forms

I have recently started using laravel and i have hit an obstacle which i have been trying to solve now for a week. I have a form which i want to validate when a user submits it. The form posts to a controller which I manually defined. When i click on the submit button i'm always being returned on the form and nothing happens to the form. I have tried different methods like using urls and routes but its its not working . Anyone to help me please. Here is some of my code:

My LogIn Form

 {{  Form::open(array('action'=>'LogInController@doLogIn', 'method' => 'post')) }}	
 <table width="439" cellpadding="5" cellspacing="0" summary="shows up when a user wants to log in the system">
  <tbody>
    <tr>
      <td height="34" colspan="2"><div align="center">
        <h1>ENTER YOUR DETAILS TO LOG IN</h1>
      </div></td>
    </tr>
    <tr>
      <td height="24"><div align="left">
        <h2>{{  Form::label('username', 'USERNAME') }}</h2>
      </div></td>
      <td height="24">{{  Form::text('username'), Input::old('username')  }} </td>
    </tr>
    <tr>
      <td width="86" height="23"><h2> {{  Form::label('password', 'PASSWORD')  }}</h2></td>
      <td width="327" height="23">{{  Form::password('password')  }} </td>
    </tr>
    <tr>
      <td height="31" colspan="2"><div align="center">
        <p>
          <label>
            <div align="center">
                <h1>
                  {{ Form::radio('staffstudent[]', 'staff', false)  }}
                  {{ Form::label('staff', 'STAFF')  }}

                  {{ Form::radio('staffstudent[]', 'student', false)  }}
                  {{ Form::label('student', 'STUDENT')  }}
                   </h1>
            </div>
          </label>
        </p>
        <div align="center">
          {{  Form::submit('Log In')  }}
        </div>
      </div></td>
    </tr>
    <tr>
      <td height="31" colspan="2"><div align="center">
        <a href="resetpassword.php"><h3>Forgot Password</h3></a>
      </div></td>
    </tr>
  </tbody>
</table> 
{{  Form::close()  }}  

My Routes

    <?php
   //route to get the log in form
   Route::get('/', array('uses' => 'HomeController@showLogIn'));

   //route to process the log in form
   Route::post('/', 'LogInController@doLogIn');

My LogInController

class LogInController extends \BaseController {


public function doLogin()
{
	// validate the info, create rules for the inputs
	$rules = array(
		'username'    => 'required|username', // make sure the username field is not empty
		'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
	);

	// run the validation rules on the inputs from the form
	$validator = Validator::make(Input::all(), $rules);

	// if the validator fails, redirect back to the form
	if ($validator->fails()) {
		return Redirect::to('/')
			->withErrors($validator) // send back all errors to the login form
			->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
	} else {

		// create our user data for the authentication
		$userdata = array(
			'username' 	=> Input::get('username'),
			'password' 	=> Input::get('password')
		);

		// attempt to do the login
		if (Auth::attempt($userdata)) {

			// validation successful!
			// redirect them to the secure section or whatever
			// return Redirect::to('secure');
			// for now we'll just echo success (even though echoing in a controller is bad)
			echo 'SUCCESS!';

		} else {	 	

			// validation not successful, send back to form	
			return Redirect::to('/');

		}

	}
}

}
Last updated 3 years ago.
0

Hmm.. you are only allowed to have one controller per route in your routes.php .

You can set your RESTful controller to a new route to reference

For example:

<?php
   //route to get the log in form
   Route::get('/', array('uses' => 'HomeController@showLogIn'));

   //route to process the log in form
   Route::post('/login', ['uses' => 'LogInController@doLogIn', 'as' => 'login.form']);
   Route::controller('/login', 'LogInController');

Then in your view change your Form::open to:

{{  Form::open([route'=>'login.form', 'method' => 'post')] }} 
Last updated 3 years ago.
0

its now showing the error 'something went wrong'

Last updated 3 years ago.
0

it is working ,i think my validation code has a problem. I commented it out and commanted to direct the user to the laravel welcome page and it worked

Last updated 3 years ago.
0

here is where I changed

  //route to process the log in form
  Route::post('/controller', array('uses' => 'HomeController@doLogIn'));

I edited the doLogIn function to

public function doLogIn()
   {
         return View::make('hello');
    }  
Last updated 3 years ago.
0

andyblem said:

its now showing the error 'something went wrong'

Go to the file 'app.php' and set debug to true to give yourself a helping hand in the future.

Last updated 3 years ago.
0

okay, thanks

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

andyblem andyblem Joined 7 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.