Laravel

Validation & Forms

Validation rules, form requests, custom rules, error messages, old input, CSRF protection, displaying errors

22 perguntas de entrevistaยท
Mid-Level
1

Which method of a Form Request returns the validation rules?

Resposta

The rules() method in a Form Request returns an associative array where each key is a field name and each value is a string or array of validation rules. This allows centralizing all validation logic outside the controller, making the code more testable and maintainable. Using Form Requests is considered a Laravel best practice for complex forms with multiple fields.

2

Which Blade directive is used to protect a form against CSRF attacks?

Resposta

The @csrf directive automatically generates a hidden field containing a security token that will be verified when the form is submitted. Laravel automatically checks this token for all POST, PUT, PATCH and DELETE requests via the VerifyCsrfToken middleware. Without this directive, requests will be rejected with a 419 error. This protection is essential to prevent Cross-Site Request Forgery attacks.

3

Which validation rule should be used to verify that a field is a valid email address?

Resposta

The email rule validates that the field value matches the standard email address format. Laravel uses PHP's filter_var function with the FILTER_VALIDATE_EMAIL filter to perform this validation. This rule checks email syntax but does not guarantee that the address actually exists. For stricter validations, it is possible to combine this rule with others like unique to verify database uniqueness.

4

How to display old form values after a validation error?

5

What is the correct syntax to define multiple validation rules on a field?

+19 perguntas de entrevista

Domine Laravel para sua proxima entrevista

Acesse todas as perguntas, flashcards, testes tecnicos, exercicios de code review e simuladores de entrevista.

Comece gratis