Unit 5: Laravel Form validation - Practice Quiz

INT221 — Mvc Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 What is the primary purpose of the CSRF token in Laravel forms?

A. To protect the application from Cross-Site Request Forgery attacks
B. To automatically populate form fields with old data
C. To validate the HTTP method used in the form
D. To encrypt form data before transmission

2 Which Blade directive is used to generate the hidden CSRF token input field inside a form?

A. @secure
B. @csrf
C. @token
D. @method

3 If a POST request is made to a Laravel route without a valid CSRF token, which HTTP status code is typically returned?

A. 500 Internal Server Error
B. 403 Forbidden
C. 404 Not Found
D. 419 Page Expired

4 HTML forms natively support which of the following HTTP verbs?

A. GET and POST only
B. GET, POST, PUT, and DELETE
C. POST and PUT only
D. GET, POST, and PATCH

5 Which Blade directive helps in performing 'Method Spoofing' to simulate PUT or DELETE requests?

A. @verb
B. @http
C. @method
D. @spoof

6 What is the name of the hidden input field used by Laravel to determine the spoofed HTTP method?

A. _verb
B. _action
C. _token
D. _method

7 In a controller, which method is most commonly called on the $request object to validate incoming data?

A. $request->check()
B. $request->verify()
C. $request->validate()
D. $request->sanitize()

8 What happens automatically when the $request->validate() method fails?

A. It returns a JSON response with the errors immediately
B. It stops execution and logs the error without redirecting
C. It redirects the user back to the previous location with errors and input flashed to the session
D. It throws a 500 error

9 Which validation rule ensures that a field must be present in the input data and not empty?

A. present
B. not_null
C. required
D. filled

10 How can you specify multiple validation rules for a single field using the pipe syntax?

A. 'title' => 'required/unique:posts/max:255'
B. 'title' => 'required & unique:posts & max:255'
C. 'title' => 'required|unique:posts|max:255'
D. 'title' => 'required, unique:posts, max:255'

11 Which data structure can be used as an alternative to the pipe syntax for defining rules?

A. JSON Object
B. Linked List
C. Comma-separated string
D. PHP Array

12 Which validation rule checks if two fields (e.g., password and password_confirmation) have the same value?

A. confirmed
B. match
C. same
D. equal

13 What does the unique:users validation rule check?

A. That the input value does not already exist in the users database table
B. That the input value is unique within the form submission
C. That the user is unique in the session
D. That the input value is unique across all tables

14 If you want a field to be optional but validated if it is present, which rule should typically accompany specific constraints?

A. optional
B. nullable
C. sometimes
D. allow_empty

15 Which variable is automatically shared with all views to display validation errors?

A. $validation
B. $errors
C. $exceptions
D. $messages

16 How do you check if there are any validation errors in a Blade view?

A. @if($errors->any())
B. @if($errors->has())
C. @if($errors->count() > 0)
D. @if(!empty($errors))

17 Which method retrieves the first error message for a specific field?

A. $errors->one('field_name')
B. $errors->get('field_name')
C. $errors->first('field_name')
D. $errors->start('field_name')

18 Which Blade directive allows you to quickly check for validation errors on a specific field and display them?

A. @haserror('email')
B. @error('email')
C. @check('email')
D. @validation('email')

19 Consider the following code inside a Blade view:

blade
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror

What does the $message variable contain?

A. The validation error message for the 'title' field
B. An array of all errors for the 'title' field
C. The name of the field ('title')
D. The validation rule that failed

20 To repopulate a form field with its previous value after a validation failure, which helper function is used?

A. back()
B. previous()
C. input()
D. old()

21 What is the second argument of the old() helper function used for? e.g. old('email', $user->email)

A. It forces the value to overwrite the old input
B. It specifies the input type
C. It specifies the validation rule
D. It sets a default value if the old input is not found

22 Which Artisan command creates a new custom validation rule class?

A. php artisan create:rule RuleName
B. php artisan make:validation RuleName
C. php artisan make:rule RuleName
D. php artisan rule:make RuleName

23 In a custom Rule class implementing Illuminate\Contracts\Validation\Rule, which method contains the validation logic?

A. validate()
B. check()
C. passes()
D. verify()

24 In a custom Rule class, what is the purpose of the message() method?

A. To send an email notification
B. To log the error to a file
C. To return a success message
D. To return the validation error message if validation fails

25 Which validation rule stops the validation process for a field immediately after the first failure?

A. halt
B. stop
C. bail
D. break

26 How would you validate that an uploaded file is an image?

A. 'photo' => 'filetype:image'
B. 'photo' => 'type:jpg,png'
C. 'photo' => 'is_image'
D. 'photo' => 'image'

27 What is the correct syntax to define a custom error message for a specific rule on a specific attribute in the validate method?

A. Pass an array as the second argument: ['email.required' => 'We need your email!']
B. Pass an array as the third argument: ['email.required' => 'We need your email!']
C. Define it in the config/app.php file
D. Use the setMessage method

28 Correction on previous logic: In $request->validate($rules, $messages), the messages array is passed as which argument?

A. Second
B. First
C. Fourth
D. Third

29 Which rule checks that a value is within a given list of values?

A. array
B. list
C. in
D. contains

30 What does the max:255 rule implies when validating a numeric field?

A. The number must have at most 255 digits
B. The string length representation must be 255 characters
C. The file size must be 255 kilobytes
D. The number must be less than or equal to 255

31 What does max:1024 imply when validating a file upload?

A. Max 1024 megabytes
B. Max 1024 kilobytes
C. Max 1024 bytes
D. Max 1024 characters in filename

32 How can you validate nested array input, for example, checking the name of every person in a people array?

A. 'people[].name' => 'required'
B. 'people:name' => 'required'
C. 'people.name' => 'required'
D. 'people.*.name' => 'required'

33 Which rule ensures a string is a valid URL?

A. http
B. link
C. website
D. url

34 Which of the following is NOT a valid way to specify a custom validation rule?

A. Extending the Validator via a Service Provider
B. Using a Closure
C. Using a Rule Object
D. Using a standard PHP if statement inside the rules array

35 When using a Closure for custom validation, how do you signal a failure?

A. Return false
B. Call the $fail callback with the error message
C. Return a string
D. Throw an Exception

36 What is the purpose of the FormRequest class in Laravel?

A. To manage database migrations for forms
B. To replace the Controller entirely
C. To handle HTML form rendering
D. To encapsulate validation and authorization logic in a dedicated class

37 In a FormRequest class, what does the authorize() method do?

A. It checks if the user has permission to make the request
B. It sets the validation error messages
C. It logs the user in
D. It validates the form data

38 Which rule validates that the field under validation matches a given regular expression?

A. regex
B. match
C. regexp
D. pattern

39 To use the regex rule inside a pipe-delimited string, what must you be careful about?

A. You cannot use regex in pipe strings
B. Regex rules must always be last
C. You must escape the pipe character if it is part of the regex
D. You must use an array instead of a pipe string to avoid conflict

40 If you want to validate a date and ensure it is after a specific date (e.g., tomorrow), which rule would you use?

A. future:true
B. date_min:tomorrow
C. later_than:tomorrow
D. after:tomorrow

41 Which method is used to retrieve validated data from a FormRequest instance?

A. $request->validated()
B. $request->all()
C. $request->input()
D. $request->checked()

42 Why might you get a TokenMismatchException?

A. The database connection failed
B. The CSRF token is missing or invalid
C. The validation rules failed
D. The user password is incorrect

43 How can you exclude specific URIs from CSRF protection?

A. Delete the VerifyCsrfToken middleware
B. It is not possible to exclude URIs
C. Use @without_csrf in the blade file
D. Add URIs to the $except array in the VerifyCsrfToken middleware

44 The accepted validation rule is commonly used for:

A. Password confirmation
B. Email validation
C. Terms of Service acceptance (checkbox)
D. File uploads

45 What is the key difference between size rule and min/max rules?

A. size validates an exact amount (characters, number, or file size), whereas min/max validate a range
B. size is only for files
C. There is no difference
D. min/max is only for numbers

46 Which rule validates that the value must exist in a given database table?

A. exists
B. database
C. has
D. contains

47 When repopulating forms, what does <input type="text" name="email" value="{{ old('email') }}"> ensure?

A. The input is disabled
B. The input shows the email currently stored in the database
C. The input retains the user's entry after a validation error redirect
D. The email is automatically validated

48 If you need to validate a boolean value, which rule should be used?

A. boolean
B. true_false
C. bool
D. is_bool

49 In a Blade template, how is the $errors variable passed to the view?

A. It is injected via the ShareErrorsFromSession middleware
B. It is created by the @csrf directive
C. It is a global PHP variable
D. You must manually pass it from the controller

50 Which validation rule is the opposite of accepted?

A. denied
B. rejected
C. refused
D. declined