I want to validate a pin code that is just for 4 digits and it should not be required. means pin code is only 4 digits. like 1234, 8675, or if the user does not enter any digits to Pincode input type let the user submit the form. I want to give the database's default value 1111 if the user doesn't enter the pin code.
CodePudding user response:
You can validate the request (or use a form request) that allows pin
to be null
otherwise validates it is a 4 digit number. You can then get the pin
from the input or fallback to the default value of 1111.
$request->validate([
'pin' => ['nullable', 'numeric', 'digits:4'],
]);
$pin = $request->input('pin', '1111');
CodePudding user response:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'pin' => ['numeric' , 'min:4', 'max:4']
]);
if ($validator->fails()) {
$pin = 1111;
}
if ($validator->validated())
{
$pin = $request->pin;
}