Laravel is a popular PHP framework that provides many convenient features to quickly build web applications. One common function is to handle form submissions. In this article, we will take a deep dive into form submission in Laravel.
First, we need to create a form in the web page. Laravel provides many form building methods, the most commonly used is to use Blade views to generate forms. The following is a simple example:
<form method="POST" action="{{ route('submit') }}"> @csrf <label for="name">姓名:</label> <input type="text" id="name" name="name"> <button type="submit">提交</button> </form>
In this example, we use the <form>
tag to create a form, and set the form's method
and action
Attribute. method
Specify the form submission method, the commonly used ones are GET
and POST
. action
Specify the target address for form submission. We can use the route
function provided by Laravel to generate the routing address.
At the same time, we also need to add a hidden field to verify the security of the form submission request. Laravel provides a @csrf
directive to generate this hidden field.
In the form, we can add various types of form elements, such as text boxes, drop-down boxes, radio buttons, check boxes, etc. These elements can be created using tags such as <input>
, <textarea>
, and <select>
.
When the user submits the form, we need to process it on the backend. Laravel provides a convenient way to handle form submission requests.
First, we need to add a method to handle requests in the routing definition, for example:
Route::post('/submit', 'FormController@submit')->name('submit');
This routing definition means that when the user accesses / in the
POST way When submitting the
address, the submit
method in FormController
will be called for processing. We also gave this route a name to facilitate generating the action
attribute of the form in the view.
Next, we need to define the submit
method in FormController
to handle the form submission request. For example:
use Illuminate\Http\Request; class FormController extends Controller { public function submit(Request $request) { $name = $request->input('name'); // 处理表单数据 return view('submit-success'); } }
In this method, we first obtain the Request
object through dependency injection. This object can be used to obtain data submitted by the form.
For example, we use $request->input('name')
to get the value of a form element named name
.
Next, we can process the form data, such as storing it in the database. Finally, we return a view to display the page where the form submission was successful.
When processing a form submission request, we usually also need to verify the user input data to ensure the legitimacy of the data. Laravel provides a very convenient way to perform form validation.
First, we need to define a rules
method in FormController
to define validation rules. For example:
class FormController extends Controller { public function rules() { return [ 'name' => 'required|max:255', 'email' => 'required|email|unique:users', 'password' => 'required|confirmed|min:6', ]; } }
In this method, we return an array, where each element represents a validation rule for a form element. For example, the validation rules for the name
element indicate that it is required and cannot be longer than 255 characters.
Next, we need to modify the submit
method and use the validate
method for form verification. For example:
public function submit(Request $request) { $validatedData = $request->validate($this->rules()); // 处理验证通过的表单数据 return view('submit-success'); }
In this method, we first call the rules
method to obtain the verification rules. Then, we called the $request->validate
method to perform form validation. If validation fails, Laravel will automatically return a response containing error information. If the verification passes, Laravel will return the verified form data, which we can use in subsequent processing.
It should be noted that the validate
method will automatically use the validation rules defined in the rules
method for verification. If there are error messages, Laravel will automatically Added to the $errors
variable, we can use $errors->first('name')
in the view to get the form element named name
the first error message.
Through the above steps, we can easily handle form submission requests in Laravel. First, we need to create the form in the view and use the Blade directive to set the form elements. Then, we add a method to handle the request in the routing definition, use the Request
object to obtain the form data, and process the data. Finally, we can use the rules
method to define form validation rules and the validate
method to perform validation.
It should be noted that Laravel provides many other ways to handle forms, such as using the Form
class and the Request
object to create a form, using Request
Object for form validation, etc. We can choose different ways to process forms according to specific needs.
The above is the detailed content of Discuss related knowledge about form submission in Laravel. For more information, please follow other related articles on the PHP Chinese website!