21
votes

I have a login form which has email and password fields. And I have two submit buttons, one for login ( if user is already registered ) and the other one for registration ( for new users ). As the login action and register action are different so I need some way to redirect the request with all the post data to its respective action. Is there a way to achieve this in Laravel 4 in pure Laravel way?

5
Give the submit buttons different names.Ali Gajani

5 Answers

38
votes

The way I would do it

If your form is (2 buttons):

{{ Form::open(array('url' => 'test/auth')) }}
{{ Form::email('email') }}
{{ Form::password('password') }}
{{ Form::password('confirm_password') }}
<input type="submit" name="login" value="Login">
<input type="submit" name="register" value="Register">
{{ Form::close() }}

Create a controller 'TestController'

Add a route

Route::post('test/auth', array('uses' => 'TestController@postAuth'));

In TestController you'd have one method that checks which submit was clicked on and two other methods for login and register

<?php

class TestController extends BaseController {

    public function postAuth()
    {
        //check which submit was clicked on
        if(Input::get('login')) {
            $this->postLogin(); //if login then use this method
        } elseif(Input::get('register')) {
            $this->postRegister(); //if register then use this method
        }

    }    

    public function postLogin()
    {
        echo "We're logging in";
        //process your input here Input:get('email') etc.
    }

    public function postRegister()
    {
        echo "We're registering";
        //process your input here Input:get('email') etc.
    }

}
?>
3
votes

My solution:

first I set up form with no action

<script>
    var baseUrl = "{{ URL::to('/'); }}";   
</script>

{{ Form::open(array('url' => '', 'id'=> 'test-form')) }}
{{ Form::text('username') }}
{{ Form::password('password') }}
{{ Form::password('confirm_password') }}
<input type="submit" name="login" id="login" value="Login">
<input type="submit" name="register" id="register" value="Register">
{{ Form::close() }}

for routes (routes.php):

Route::post('test/login', array('uses' => 'TestController@login'));
Route::post('test/register', array('uses' => 'TestController@register'));

controller (TestController.php)

public function login(){
    //do something...
}

public function register(){
    //do something...
}

Jquery: (test.js)

$('form :submit').on('click', function(event){

    var a = $(this);
    var form = $('form');
    var action = a.attr('id');

    form.attr('action', baseUrl + '/test/' + action);
    form.submit();
});
3
votes

ClickCoder's answer is spot-on, the only thing I would replace, to do it in an 100% "pure Laravel way" would be to use the Form Builder for the submit buttons as well.

http://laravel.com/docs/4.2/html#buttons

The first parameter is the submit's value, and you can pass its name in the following options array.

So instead of:

<input type="submit" name="login" value="Login">
<input type="submit" name="register" value="Register">

You'd do:

{{ Form::submit('Login', ['name' => 'login']) }}
{{ Form::submit('Register', ['name' => 'register']) }}

If you'd like, you could escape the if statement altogether by having them share the same name and do something like this:

html:

{{ Form::submit('Login', ['name' => 'action']) }}
{{ Form::submit('Register', ['name' => 'action']) }}

php:

public function postAuth()
{
    $action = 'post' . Input::get('action'); //becomes either postLogin or postRegister
    $this->$action();
}

A bit less nesting, a bit more Laravel!

0
votes

You can do like the following:

Your routes (routes.php):

Route::post('post1', array('before' => 'checkForm', 'uses' => 'TestController@post1'));

Route::get('post2', array('as' => 'post2', 'uses' => 'TestController@post2'));

filters.php

Route::filter('checkForm', function()
{
    if(Input::has('button2')) {
        return Redirect::route('post2')->withInput();
    } 
});

TestController.php

public function post1(){
        return Input::all();
    }

    public function post2(){
        return Input::old();
    }

Form: should be in your view.however, I have created a temp form in route file for this answer.

Route::get('form2', function() 
{
      return Form::open(array('url' => URL::to('post1'))).
             Form::text('name', null).
             Form::email('email', null).
             '<button type="submit" name="button1" value="1">submit</button>'.
             '<button type="submit" name="button2" value="2">submit2</button>'.
           Form::close();

});

now browse:

http://yourDomain/form2

Please note: I am not sure this is the right approach to allow two submit buttons but it work for two submit buttons.

0
votes

Give the submit buttons different names, eg

<button type="submit" name="login">Login</button> 
<button type="submit" name="signup">Signup</button>

In your controller, simply handle login or signup depending on which button was submitted. You will still get all your inputs, you can do this;

public function postLoginOrSignup(){

    $data = Input::all();

    if(isset($data['login'])){
        //handle login process
    }

    if(isset($data['signup'])){
        //handle singup process
    }

}