The required_without
rule is used for form validation in Laravel. This rule is similar to frontend checks that mark form input fields as required.
required_without
validation ruleThe required_without
validation rule can be seen as an OR
in a query language statement, in which execution continues if any condition is met.
The code below shows how to use the required_without
validation rule:
APP_NAME=Laravel APP_ENV=local APP_KEY= APP_DEBUG=true APP_URL=http://localhost LOG_CHANNEL=stack LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= BROADCAST_DRIVER=log CACHE_DRIVER=file FILESYSTEM_DRIVER=local QUEUE_CONNECTION=sync SESSION_DRIVER=file SESSION_LIFETIME=120 MEMCACHED_HOST=127.0.0.1 REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_MAILER=smtp MAIL_HOST=mailhog MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS=null MAIL_FROM_NAME="${APP_NAME}" AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= AWS_DEFAULT_REGION=us-east-1 AWS_BUCKET= AWS_USE_PATH_STYLE_ENDPOINT=false PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_APP_CLUSTER=mt1 MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
On lines 12-13, we set first_name
as required_without:last_name
and last_name
as required_without:first_name
. It includes three validation cases:
If the first_name
field is filled and the last_name
field is empty, then execution continues.
If the first_name
field is empty and the last_name
field is filled, the execution again continues.
There is only an error when both the first_name
and last_name
fields are empty.
In Laravel, the required_without
validation rule is used to ensure that a field is required if none of the other specified fields are filled. It allows us to define dependencies between fields, indicating that a particular field must be filled when other related fields are empty.
Free Resources