What is the purpose of the @csrf directive in Laravel forms?
The @csrf directive adds a hidden token to the form to protect against Cross-Site Request Forgery attacks. It ensures the form submission is secure and comes from your application.
Click to reveal answer
beginner
How do you retrieve form input data in a Laravel controller?
You use the $request->input('field_name') method or simply $request->field_name to get the value of a form input named field_name.
Click to reveal answer
beginner
What HTML element is used to create a text input field in a Laravel Blade form?
You use the <input type="text" name="field_name" /> element. Laravel also offers helpers like Form::text('field_name') if using the Form package.
Click to reveal answer
intermediate
How can you repopulate old input values in a Laravel form after validation fails?
Use the old('field_name') helper inside the value attribute, like <input type="text" name="field_name" value="{{ old('field_name') }}" />. This keeps user input after errors.
Click to reveal answer
intermediate
What method should the form use to send data securely for creating or updating resources in Laravel?
Use the POST method for creating data and PUT or PATCH for updating. In HTML forms, use method="POST" and add @method('PUT') or @method('PATCH') for updates.
Click to reveal answer
Which directive adds a CSRF token to a Laravel form?
A@token
B@method
C@csrf
D@form
✗ Incorrect
The @csrf directive adds a hidden CSRF token input to protect the form from cross-site request forgery.
How do you access the value of a form input named 'email' in a Laravel controller?
A$request->email
B$request->getEmail()
C$request->inputEmail()
D$request->form['email']
✗ Incorrect
You can access input values directly using $request->email or $request->input('email').
What HTML attribute is essential to identify form inputs in Laravel?
Aname
Bid
Cclass
Dvalue
✗ Incorrect
The 'name' attribute is used to identify form inputs and retrieve their values in Laravel.
Which helper repopulates old input after validation errors?
Aprevious()
Bback()
Cinput()
Dold()
✗ Incorrect
The old() helper returns the previous input value to keep user data after validation fails.
To update a resource via a form in Laravel, which method should you spoof?
APOST
BPUT
CGET
DDELETE
✗ Incorrect
HTML forms only support GET and POST, so you spoof PUT or PATCH using @method('PUT') for updates.
Explain how to create a secure form in Laravel that keeps user input after validation errors.
Think about security and user experience when submitting forms.
You got /4 concepts.
Describe how Laravel handles HTTP methods in forms for creating and updating data.
Remember HTML form limitations and Laravel's method spoofing.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of adding a name attribute to an input field in a Laravel form?
easy
A. It identifies the input data when the form is submitted
B. It styles the input field with CSS
C. It disables the input field
D. It adds a placeholder text inside the input
Solution
Step 1: Understand the role of the name attribute
The name attribute is used to label the input data so the server can identify it when the form is submitted.
Step 2: Recognize other options are unrelated to data submission
Styling, disabling, or placeholder text do not affect how data is sent to the server.
Final Answer:
It identifies the input data when the form is submitted -> Option A
Quick Check:
Input name = data key [OK]
Hint: Name attribute links input to server data key [OK]
Common Mistakes:
Confusing name with styling or placeholder
Thinking name disables input
Ignoring name causes missing data on submit
2. Which of the following is the correct way to include CSRF protection in a Laravel Blade form?
easy
A. <form> @csrf </form>
B. <form> <csrf> </form>
C. <form> <input type='csrf'> </form>
D. <form> <token> @csrf </form>
Solution
Step 1: Recall Laravel's CSRF directive
Laravel uses the Blade directive @csrf inside forms to add a hidden CSRF token input automatically.
Step 2: Check syntax correctness
Only @csrf is valid; other tags like <csrf> or <token> are invalid HTML or Blade syntax.
Final Answer:
<form> @csrf </form> -> Option A
Quick Check:
Use @csrf in Blade forms [OK]
Hint: Use @csrf inside Blade forms for security [OK]
Laravel requires a CSRF token in POST forms to prevent attacks. This form lacks @csrf.
Step 2: Validate other parts
Input type 'text' is allowed for emails (though 'email' is better), action URL is valid, and name attribute is required.
Final Answer:
Missing @csrf directive for security -> Option D
Quick Check:
POST forms need @csrf [OK]
Hint: Always add @csrf in POST forms [OK]
Common Mistakes:
Forgetting @csrf causes 419 error
Thinking input type must be 'email' for emails
Assuming action URL must be absolute
5. You want to create a form that submits a user's age but only accepts numbers between 18 and 99. Which is the best way to enforce this in a Laravel Blade form input?
hard
A. <input type="text" name="age" pattern="[18-99]">
B. <input type="number" name="age" min="18" max="99" required>
C. <input type="number" name="age" min="18" max="99">
D. <input type="text" name="age" required>
Solution
Step 1: Use input type number with min and max attributes
Setting type="number" with min="18" and max="99" restricts input to numbers in that range.
Step 2: Add required to ensure user cannot submit empty
This makes the field mandatory, improving validation before submission.
Final Answer:
<input type="number" name="age" min="18" max="99" required> -> Option B
Quick Check:
Number input with min/max and required [OK]
Hint: Use number input with min, max, and required [OK]
Common Mistakes:
Using text input with incorrect pattern syntax
Omitting required allows empty submission
Using min/max without input type number
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.