In this tutorial we will see form input validation rules in laravel.
Form input validation rules in laravel
Laravel provides many type of validation rules by which you can validate user input.
Some of the commonly used validation rules in Laravel are:
- required: The field is required.
- filled: The field is required and not null.
- email: The field must be a valid email address.
- numeric: The field must be a numeric value.
- integer: The field must be an integer value.
- string: The field must be a string.
- array: The field must be an array.
- boolean: The field must be a boolean value.
- date: The field must be a valid date.
- date_format:format: The field must match the given date format.
- after:date: The field must be after the given date.
- before:date: The field must be before the given date.
- size:value: The field must be the given size.
- min:value: The field must be at least the given value.
- max:value: The field must not exceed the given value.
- unique:table,column,except,idColumn: The field must be unique in the specified table and column, except for the specified ID column (optional).
- exists:table,column: The field value must exist in the specified table and column.
- regex:pattern: The field must match the given regular expression pattern.
- alpha: The field must contain only letters (a-zA-Z).
- alpha_dash: The field may contain letters, numbers, dashes, and underscores.
- alpha_num: The field may contain only letters and numbers.
- distinct: The field values must be unique.
- in:foo,bar,...: The field value must be one of the specified values.
- not_in:foo,bar,...: The field value must not be one of the specified values.
- confirmed: The field must have a matching confirmation field (e.g. password and password_confirmation).
- gt:field: The field must be greater than the specified field.
- gte:field: The field must be greater than or equal to the specified field.
- lt:field: The field must be less than the specified field.
- lte:field: The field must be less than or equal to the specified field.
- json: The field must be a valid JSON string.
- mimetypes:text/plain,...: The field must be one of the specified MIME types.
- file: The field must be a valid file.
- image: The field must be a valid image file.
- timezone: The field must be a valid timezone identifier.
- url: The field must be a valid URL.
- active_url: The field must be a valid URL with a DNS record.
- regex:/^[a-zA-Z0-9\s]+$/: The field must match the given regular expression pattern.
- digits:value: The field must be numeric and have an exact length of value.
- digits_between:min,max: The field must be numeric and have a length between min and max.
- size:value: The field must have a file size equal to value (in kilobytes).
- dimensions:min_width=min, max_width=max,min_height=min, max_height=max: The field must have dimensions within the specified range.
- nullable: The field may be null. This rule is often used in combination with other rules to allow for optional input.
- sometimes: The field will only be validated if it is present in the input array. This rule is often used when you have optional input fields that should be validated if they are present.
- prohibited: The field is prohibited from being included in the input data. This rule can be useful if you want to explicitly disallow certain fields from being submitted.
- prohibited_if:anotherfield,value: The field is prohibited from being included in the input data if anotherfield has the given value.
- required_if:anotherfield,value: The field is required if anotherfield has the given value.
- required_unless:anotherfield,value: The field is required unless anotherfield has the given value.
- required_with:foo,bar,...: The field is required if any of the specified fields are present in the input data.
- required_with_all:foo,bar,...: The field is required if all of the specified fields are present in the input data.
- required_without:foo,bar,...: The field is required if none of the specified fields are present in the input data.
- required_without_all:foo,bar,...: The field is required if none of the specified fields are present in the input data.
- starts_with:value1,value2,...: The field value must start with one of the specified values.
- ends_with:value1,value2,...: The field value must end with one of the specified values.
- nullable_if:anotherfield,value: The field may be null if anotherfield has the given value.
- nullable_unless:anotherfield,value: The field may be null unless anotherfield has the given value.
- after:date: The field must be a date that comes after the given date.
- after_or_equal:date: The field must be a date that comes after or is equal to the given date.
- before:date: The field must be a date that comes before the given date.
- before_or_equal:date: The field must be a date that comes before or is equal to the given date.
- accepted: The field must be accepted (e.g. a checkbox that must be checked).