Input is Array Helper in laravel 4

Input is Array Helper

It is a very important idea in laravel 4 … for me, if you understand it you will find out that Laravel is a very easy to understand and customizable framework. Recently, I am doing some research on how laravel 4 working in validation. It is the most simple and common validation code that we always use.

$rules = array(
		"username" => "required|min:3",
		"password" => "required|min:3",
	);

$validate = Validator::make(Input::all(), $rules);
if($validate->fails()){
	return Redirect::to("/service-download/login")
		->withInput(Input::except("password"))
		->withErrors($validate);
}
@if(!empty($errors->all())
    {{Html::ul($errors->all())}}
@endif
<form method="post">
    <input type="hidden" name="_token" value="secret">
    <input type="text" name="username" value="{{Input::old("username")}}>
    <input type="password" name="password">
    <input type="submit" value="Submit">
</form>

In code above, we are using three different Input static methods, Input::old(), Input::get() and Input::all(). The results we get respectively are…

// Input::all()
array(
    "_token" => "secret",
    "username" => "my name",
    "password" => "my_pass", 
);

// Input::old("username")
"my name"

// Input::except("password")
array(
    "_token" => "secret",
    "username" => "my name",
);

It means, Input returned in array with attribute key instead of … eh.. maybe Input object? We can simply create input result or event modify the input. What I have always do is modify the Input in order to adapt to rules.

$rules = array(
		"username" => "required|min:3",
		"password" => "required|min:3",
	);

$input = Input::all();
$validate = Validator::make($input, $rules);

$input["form_type"] = 1;
Session::put("temp_form",$input);

if($validate->fails()){
	return Redirect::to("/service-download/login")
		->withInput(Session::get("temp_form"))
		->withErrors($validate);
}

Why am I doing so? I have one or more confirm pages before saving the form data and multiple type of form, I need form_type to identify which type of form and storing data for multiple confirm pages.

Input returned me data in arrays so it can be used directly with Session.

Input::all

Returned all data retrieved in post method and get method in term of Array.

Input::only

Return array with specify keys.

// Input::only(array("username","gender","birthday"))
array(
    "username" => "my name",
    "gender" => "m",
    "birthday" => "2004-9-1",
);

Input::old

Return mixed type data by key given in 1st parameter. If key is not found from arrays or value is empty, 2nd parameter will be returned.

// Input::old("gender","male")
"male"

Input::has

Same as array_key_exists. It might looks like this.

// Input::has("username")
array_key_exists("username", Input::all())

Conclusions

Why am I saying “Input is Array Helper”? Request can be retrieve by Input::all() in term of array. You can easily modify the request by modify the array. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s