Laravel

Validation & Forms

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

22 면접 질문·
Mid-Level
1

Which method of a Form Request returns the validation rules?

답변

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?

답변

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?

답변

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 면접 질문

다음 면접을 위해 Laravel을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기