laravael 9 Validation Segment with some condition - php

I am working on the online application. it has several sections. some of the sections are shown in some conditions only. some of the validation rules also apply in the dynamic section .how do i segment the validation rule based on the condition.I try following the way and it does not work for me.
$validatedData = $request->validate([
'titel_id' => ['required'],
'initials' => ['required','regex:/^[A-Za-z.]+$/'],
'name_denoted_by_initials' => 'required',
'last_name' => 'required|alpha',
'phone_no' => 'max:12',
/********************************* */
],[
'titel_id.required' => 'the title required',
'initials.required' => 'the name initials required',
'initials.regex' => 'initials only can include letters and dots',
'name_denoted_by_initials.required' => 'the initials denoted name required',
'last_name.required' => 'the last name required',
'phone_no.max' => 'you entered phone number invalid',
]);
if($vacancy->main_category_id == 46){
$validatedData = $request->validate([
'index_no' => 'required',
'year' => 'required|max:'.(date('Y')),
],[
'index_no.required' => 'O/l Exam number is required',
'year.required' => 'O/l Exam year is required',
]);
}
this only shows the without condition validation rule only. how do I manage it?

You can use sometimes() which helps to create Complex Conditional Validation
$validator = Validator::make($request->all(), [
# default validations
]);
$validator->sometimes('index_no', 'required', function ($vacancy) {
return $vacancy->main_category_id === 46;
});
$validator->sometimes('year', 'required|max:'.(date('Y')), function ($vacancy) {
return $vacancy->main_category_id === 46;
});
Check how to use Manually Creating Validators
Update
Saw in the old Laravel doc. If both fields use the same validation, you can merge them.
$validator->sometimes(['index_no', 'year'], 'required', function ($vacancy) {
return $vacancy->main_category_id === 46;
});
Method 02
Create formRequest and validate
php artisan make:request VecancyRequest
In VecancyRequest.php
class VecancyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
if ($this->get('main_category_id') === 46) {
$rules['index_no'] = 'required';
$rules['year'] = 'required|max:' . (date('Y'));
}
$rules['titel_id'] = 'required';
$rules['initials'] = 'required|regex:/^[A-Za-z.]+$/';
# other validations
return $rules;
}
public function messages()
{
return [
'index_no.required' => 'O/l Exam number is required',
'year.required' => 'O/l Exam year is required',
'titel_id.required' => 'the title required',
'initials.required' => 'the name initials required',
'initials.regex' => 'initials only can include letters and dots',
'name_denoted_by_initials.required' => 'the initials denoted name required',
'last_name.required' => 'the last name required',
'phone_no.max' => 'you entered phone number invalid',
];
}
}
In controller
public function store(VecancyRequest $request)
Remove all validations from the controller. Form Request do the trick

Related

What is wrong with this form validation

I'm working with Laravel 5.8 and I have made this Controller method for creating some records inside the DB.
public function doTheUpload(Request $request)
{
try{
$request->validate([
'video' => 'nullable|mimes:mp4',
'video_thumb' => 'required|mimes:jpg,png,jpeg',
'video_name' => 'required',
'video_desc' => 'nullable',
'available_download' => 'nullable',
],[
'video.mimes' => 'video file format is not valid',
'video_thumb.required' => 'uploading video thumbnail is required',
'video_name.required' => 'you must enter name of video',
'video_thumb.mimes' => 'image thumbnail file format is not valid',
]);
// Do the upload process
}catch(\Exception $e){
dd($e);
}
}
But this will not working and return this error:
The given data was invalid.
This is basically because of the form validation requests and when I remove those validations from the method, it will work absolutely fine.
So what is wrong with those form request validation that returns this error?
If you know, please let me know... I would really really appreciate any idea or suggestion from you guys.
Thanks.
When you use Laravel's validation, you should let Laravel handle the errors, because when a rule fails, Laravel automatically throws an exception.
So the first advise is no to use a try-catch block in your validation routine.
As Laravel docs states:
Displaying The Validation Errors
So, what if the incoming request parameters do not pass the given
validation rules? As mentioned previously, Laravel will automatically
redirect the user back to their previous location. In addition, all of
the validation errors will automatically be flashed to the session.
In addition, I suggest you not to use validation in the controllers because according to good practices, it is recommended to create separate formRequest for validation, so you should slightly modify you controller to include validator class:
<?php
namespace App\Http\Controllers;
...
use App\Http\Requests\UploadVideoRequest;
...
public function doTheUpload(UploadVideoRequest $request)
{
/*
* Here where are calling validation as UploadVideoRequest
*/
// logic for valid uploaded video
}
Now you have to create a form request, maybe using php artisan make:request UploadVideoRequest
This command will create a form request class under app/Http/Requests, and you should fill it as:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UploadVideoRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
/*
* here you should check if the user is authorized to upload video
* or let in true if anyone can do that
*/
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'video' => 'nullable|mimes:mp4',
'video_thumb' => 'required|mimes:jpg,png,jpeg',
'video_name' => 'required',
'video_desc' => 'nullable',
'available_download' => 'nullable',
];
}
/**
* Define messages to return if an error is detected.
*
* #return array
*/
public function messages()
{
return [
'video.mimes' => 'video file format is not valid',
'video_thumb.required' => 'uploading video thumbnail is required',
'video_name.required' => 'you must enter name of video',
'video_thumb.mimes' => 'image thumbnail file format is not valid',
];
}
}
By using this approach Laravel is validating user input and managing any error via Exceptions.
Regards.
You must specify exactly what you are validating:
$request->validate([
'request.video' => 'nullable|mimes:mp4',
'request.video_thumb' => 'required|mimes:jpg,png,jpeg',
'request.video_name' => 'required',
'request.video_desc' => 'nullable',
'request.available_download' => 'nullable',
], [
'request.video.mimes' => 'video file format is not valid',
'request.video_thumb.required' => 'uploading video thumbnail is required',
'request.video_name.required' => 'you must enter name of video',
'request.video_thumb.mimes' => 'image thumbnail file format is not valid',
]);
An example from my codes:
$requestData = $request->request_data;
// data
$company_name = $requestData['company_name'];
$company_type = $requestData['company_type'];
$company_address = $requestData['company_address'];
$latitude = $requestData['latitude'];
$longitude = $requestData['longitude'];
$company_branch_count = $requestData['company_branch_count'];
$yes_radio = strval($requestData['yes_radio']);
$no_radio = strval($requestData['no_radio']);
$company_contact_user_first_name = $requestData['company_contact_user_first_name'];
$company_contact_user_last_name = $requestData['company_contact_user_last_name'];
$company_contact_user_email = $requestData['company_contact_user_email'];
$company_contact_user_password = $requestData['company_contact_user_password'];
$company_contact_user_phone = $requestData['company_contact_user_phone'];
$company_kvkk_ok = strval($requestData['company_kvkk_ok']);
$shipping_method_yourself = $yes_radio === 'true' && $yes_radio != $no_radio ? 1 : 0;
if ($company_kvkk_ok == 'false') {
return json_encode([
'operation_status' => 'error',
'error_messages' => 'no',
]);
}
// Validate
$validator = Validator::make($request->all(), [
"request_data.company_name" => "required|string|min:5",
"request_data.company_type" => "required|in:0,1,2,3,4,5,6,7,8,9,10,11,12",
"request_data.company_address" => "required",
"request_data.latitude" => "required",
"request_data.longitude" => "required",
"request_data.company_branch_count" => "required|integer",
"request_data.yes_radio" => "required",
"request_data.no_radio" => "required",
"request_data.company_contact_user_first_name" => "required",
"request_data.company_contact_user_last_name" => "required",
"request_data.company_contact_user_email" => [
'required',
'email',
Rule::unique('users', 'email')->where(function ($query) use ($company_contact_user_email) {
return $query->where('email', $company_contact_user_email);
}),
Rule::unique('companies', 'company_contact_user_email')->where(function ($query) use ($company_contact_user_email) {
return $query->where('company_contact_user_email', $company_contact_user_email);
}),
],
"request_data.company_contact_user_password" => "required|min:6",
"request_data.company_contact_user_phone" => "required",
"request_data.company_kvkk_ok" => "required",
], [
'request_data.company_name.required' => __('company name required'),
'request_data.company_name.string' => __('company name must be string'),
'request_data.company_name.min' => __('company name must be at least 5 characters'),
'request_data.company_type.required' => __('company type required'),
'request_data.company_type.in' => __('company type invalid'),
'request_data.company_address.required' => __('company address required'),
'request_data.latitude.required' => __('latitude required'),
'request_data.longitude.required' => __('longitude required'),
'request_data.company_branch_count.required' => __('company branch count required'),
'request_data.company_branch_count.integer' => __('company branch count must be integer'),
'request_data.yes_radio.required' => __('yes radio required'),
'request_data.no_radio.required' => __('no radio required'),
'request_data.company_contact_user_first_name.required' => __('company contact user first name required'),
'request_data.company_contact_user_last_name.required' => __('company contact user last name required'),
'request_data.company_contact_user_email.required' => __('company contact user email required'),
'request_data.company_contact_user_email.email' => __('company contact user email invalid'),
'request_data.company_contact_user_email.unique' => __('email already taken'),
'request_data.company_contact_user_password.required' => __('company contact user password required'),
'request_data.company_contact_user_password.min' => __('company contact user password must be at least 6 characters'),
'request_data.company_contact_user_phone.required' => __('company contact user phone required'),
'request_data.company_kvkk_ok.required' => __('company kvkk ok required'),
]);
if ($validator->fails()) {
$messages = $validator->messages();
return json_encode([
'operation_status' => 'not_validated',
'request' => $requestData,
'messages' => $messages,
]);
}

How to define field specific error message or overwrite the default one for a custom validation rule used in a FormRequest in Laravel 5.5

I have created a custom validation rule and I am using it to validate a data field in a FormRequest.
However, I would like to use a different message for this field and not the default message that is set in the Rule's message() method.
So, I tried using the method messages() inside of the FormRequest using a key of the field name and the rule (in snake case)
public function rules()
{
return [
'clients' => [
new ClientArrayRule(),
],
];
}
public function messages()
{
return [
'clients.client_array_rule' => "clients should be a valid client array"
];
}
The error message did not change, I investigated a little inside the code of the validator and I found out that for custom rules, it uses the method validateUsingCustomRule that doesn't seem to care about custom messages.
Any ideas on how it can be overwritten or what would be the best way to do it?
For custom message on Laravel:
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'username' => 'required',
'password' => 'required',
'email' => 'required',
'age' => 'required|gte:4',
];
}
/**
* Get the error messages for the defined validation rules.
*
* #return array
*/
public function messages()
{
return [
'username.required' => 'Custom message',
'password.required' => 'Custom message',
'email.required' => 'Custom message',
'age.required' => 'Custom message',
'age.gte' => 'Custom message'
];
}

Custom Laravel validation messages

I'm trying to create customized messages for validation in Laravel 5. Here is what I have tried so far:
$messages = [
'required' => 'Harap bagian :attribute di isi.',
'unique' => ':attribute sudah digunakan',
];
$validator = Validator::make($request->all(), [
'username' => array('required','unique:Userlogin,username'),
'password' => 'required',
'email' => array('required','unique:Userlogin,email'),$messages
]);
if ($validator->fails()) {
return redirect('/')
->withErrors($validator) // send back all errors to the login form
->withInput();
} else {
return redirect('/')
->with('status', 'Kami sudah mengirimkan email, silahkan di konfirmasi');
}
But it's not working. The message is still the same as the default one. How can I fix this, so that I can use my custom messages?
Laravel 5.7.*
Also You can try something like this. For me is the easiest way to make custom messages in methods when you want to validate requests:
public function store()
{
request()->validate([
'file' => 'required',
'type' => 'required'
],
[
'file.required' => 'You have to choose the file!',
'type.required' => 'You have to choose type of the file!'
]);
}
If you use $this->validate() simplest one, then you should write code something like this..
$rules = [
'name' => 'required',
'email' => 'required|email',
'message' => 'required|max:250',
];
$customMessages = [
'required' => 'The :attribute field is required.'
];
$this->validate($request, $rules, $customMessages);
You can provide custom message like :
$rules = array(
'URL' => 'required|url'
);
$messages = array(
'URL.required' => 'URL is required.'
);
$validator = Validator::make( $request->all(), $rules, $messages );
if ( $validator->fails() )
{
return [
'success' => 0,
'message' => $validator->errors()->first()
];
}
or
The way you have tried, you missed Validator::replacer(), to replace the :variable
Validator::replacer('custom_validation_rule', function($message, $attribute, $rule, $parameters){
return str_replace(':foo', $parameters[0], $message);
});
You can read more from here and replacer from here
For Laravel 8.x, 7.x, 6.x
With the custom rule defined, you might use it in your controller validation like so :
$validatedData = $request->validate([
'f_name' => 'required|min:8',
'l_name' => 'required',
],
[
'f_name.required'=> 'Your First Name is Required', // custom message
'f_name.min'=> 'First Name Should be Minimum of 8 Character', // custom message
'l_name.required'=> 'Your Last Name is Required' // custom message
]
);
For localization you can use :
['f_name.required'=> trans('user.your first name is required'],
Hope this helps...
$rules = [
'username' => 'required,unique:Userlogin,username',
'password' => 'required',
'email' => 'required,unique:Userlogin,email'
];
$messages = [
'required' => 'The :attribute field is required.',
'unique' => ':attribute is already used'
];
$request->validate($rules,$messages);
//only if validation success code below will be executed
//Here is the shortest way of doing it.
$request->validate([
'username' => 'required|unique:Userlogin,username',
'password' => 'required',
'email' => 'required|unique:Userlogin,email'
],
[
'required' => 'The :attribute field is required.',
'unique' => ':attribute is already used'
]);
//The code below will be executed only if validation is correct.
run below command to create a custom rule on Laravel
ı assuming that name is CustomRule
php artisan make:rule CustomRule
and as a result, the command was created such as PHP code
if required keyword hasn't on Rules,That rule will not work
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CustomRule implements Rule
{
/**
* Create a new rule instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
//return true or false
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The validation error message.';
}
}
and came time using that
first, we should create a request class if we have not
php artisan make:request CustomRequest
CustomRequest.php
<?php
namespace App\Http\Requests\Payment;
use App\Rules\CustomRule;
use Illuminate\Foundation\Http\FormRequest;
class CustomRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules(): array
{
return [
'custom' => ['required', new CustomRule()],
];
}
/**
* #return array|string[]
*/
public function messages(): array
{
return [
'custom.required' => ':attribute can not be empty.',
];
}
}
and on your controller, you should inject custom requests to the controller
your controller method
class FooController
{
public function bar(CustomRequest $request)
{
}
}
You can also use the methods setAttributeNames() and setCustomMessages(),
like this:
$validation = Validator::make($this->input, static::$rules);
$attributeNames = array(
'email' => 'E-mail',
'password' => 'Password'
);
$messages = [
'email.exists' => 'No user was found with this e-mail address'
];
$validation->setAttributeNames($attributeNames);
$validation->setCustomMessages($messages);
For those who didn't get this issue resolve (tested on Laravel 8.x):
$validated = Validator::make($request->all(),[
'code' => 'required|numeric'
],
[
'code.required'=> 'Code is Required', // custom message
'code.numeric'=> 'Code must be Number', // custom message
]
);
//Check the validation
if ($validated->fails())
{
return $validated->errors();
}
$rules = [
'name' => 'required',
'email' => 'required|email',
'message' => 'required|max:250',
];
$customMessages = [
'required' => 'The :attribute field is required.',
'max' => 'The :attribute field is may not be greater than :max.'
];
$this->validate($request, $rules, $customMessages);
In the case you are using Request as a separate file:
public function rules()
{
return [
'preparation_method' => 'required|string',
];
}
public function messages()
{
return [
'preparation_method.required' => 'Description is required',
];
}
Tested out in Laravel 6+
you can customise the message for different scenarios based on the request.
Just return a different message with a conditional.
<?php
namespace App\Rules;
use App\Helpers\QueryBuilderHelper;
use App\Models\Product;
use Illuminate\Contracts\Validation\Rule;
class ProductIsUnique implements Rule
{
private array $attributes;
private bool $hasAttributes;
/**
* Create a new rule instance.
*
* #return void
*/
public function __construct(array $attributes)
{
$this->attributes = $attributes;
$this->hasAttributes = true;
}
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
$brandAttributeOptions = collect($this->attributes['relationships']['brand-attribute-options']['data'])->pluck('id');
$query = Product::query();
$query->when($brandAttributeOptions->isEmpty(), function ($query) use ($value) {
$query->where('name', $value);
$this->hasAttributes = false;
});
return !$query->exists();
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return ($this->hasAttributes) ? 'The Selected attributes & Product Name are not unique' : 'Product Name is not unique';
}
}
Laravel 10.x
If you are using Form Requests, add another method called messages(): array in your request.
class YourRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => 'required',
'email' => 'required|email',
...
];
}
//Add the following method
public function messages(): array
{
return [
'email.required' => 'Custom message for Email Required',
];
}
}
Then the message will be displayed automatically once the request is send from the form.

Yii2: Either one field is required Validation

I have to implement the validation as mentioned in the title that either one of the two fields (email, phone) is required. I am doing this in my model:
[['email'],'either', ['other' => ['phone']]],
And this is the method:
public function either($attribute_name, $params) {
$field1 = $this->getAttributeLabel($attribute_name);
$field2 = $this->getAttributeLabel($params['other']);
if (empty($this->$attribute_name) && empty($this->$params['other'])) {
$this->addError($attribute_name, Yii::t('user', "either {$field1} or {$field2} is required."));
return false;
}
return true;
}
When I access my index page, it gives me this error:
Exception (Unknown Property) 'yii\base\UnknownPropertyException' with
message 'Setting unknown property: yii\validators\InlineValidator::0'
Any help?
If you don't care that both fields show an error when the user provides neither of both fields:
This solutions is shorter than the other answers and does not require a new validator type/class:
$rules = [
['email', 'required', 'when' => function($model) { return empty($model->phone); }],
['phone', 'required', 'when' => function($model) { return empty($model->email); }],
];
If you want to have a customized error message, just set the message option:
$rules = [
[
'email', 'required',
'message' => 'Either email or phone is required.',
'when' => function($model) { return empty($model->phone); }
],
[
'phone', 'required',
'message' => 'Either email or phone is required.',
'when' => function($model) { return empty($model->email); }
],
];
The rule should be:
['email', 'either', 'params' => ['other' => 'phone']],
And method:
public function either($attribute_name, $params)
{
$field1 = $this->getAttributeLabel($attribute_name);
$field2 = $this->getAttributeLabel($params['other']);
if (empty($this->$attribute_name) && empty($this->{$params['other']})) {
$this->addError($attribute_name, Yii::t('user', "either {$field1} or {$field2} is required."));
}
}
Improved variant
['gipsy_team_name', 'either', 'skipOnEmpty'=>false, 'params' => ['other' => 'poker_strategy_nick_name']],
['vkontakte', 'either', 'skipOnEmpty'=>false, 'params' => ['other' => ['odnoklasniki','odnoklasniki']]],
Added 'skipOnEmpty'=>false for forcing validating and 'other' can be array
/**
* validation rule
* #param string $attribute_name
* #param array $params
*/
public function either($attribute_name, $params)
{
/**
* validate actula attribute
*/
if(!empty($this->$attribute_name)){
return;
}
if(!is_array($params['other'])){
$params['other'] = [$params['other']];
}
/**
* validate other attributes
*/
foreach($params['other'] as $field){
if(!empty($this->$field)){
return;
}
}
/**
* get attributes labels
*/
$fieldsLabels = [$this->getAttributeLabel($attribute_name)];
foreach($params['other'] as $field){
$fieldsLabels[] = $this->getAttributeLabel($field);
}
$this->addError($attribute_name, \Yii::t('poker_reg', 'One of fields "{fieldList}" is required.',[
'fieldList' => implode('"", "', $fieldsLabels),
]));
}

How to use sometimes rule in Laravel 5 request class

I have the following request class:
<?php namespace App\Http\Requests\User;
use App\Http\Requests\Request;
use Validator;
use Session;
use Auth;
use App\User;
class RegisterStep1Request extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Set up the validation rules
*/
public function rules()
{
Validator::extend('valid_date', function($attribute, $value, $parameters)
{
$pieces = explode('/', $value);
if(strpos($value, '/')===FALSE) {
return false;
} else {
if(checkdate($pieces[1], $pieces[0], $pieces[2])) {
return true;
} else {
return false;
}
}
});
return [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|unique:users,email',
'dob' => 'required|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/|valid_date',
'mobile' => 'required',
'password' => 'required|confirmed'
];
}
public function messages()
{
return [
'first_name.required' => 'The first name field is required.',
'last_name.required' => 'The last name field is required.',
'email.required' => 'The email address field is required.',
'email.email' => 'The email address specified is not a valid email address.',
'email.unique' => 'The email address is already registered with this website.',
'dob.required' => 'The date of birth field is required.',
'dob.regex' => 'The date of birth is invalid. Please use the following format: DD/MM/YYYY.',
'dob.valid_date' => 'The date of birth is invalid. Please check and try again.',
'mobile.required' => 'The mobile number field is required.',
'password.required' => 'The password field is required.',
'password.confirmed' => 'The confirm password field does not match the password field.'
];
}
}
I want to add the following sometimes rule:
Validator::sometimes('dob', 'valid_date', function($input)
{
return apply_regex($input->dob) === true;
});
How would I add this to my request class?
I have amended my rules method to the following:
public function rules()
{
Validator::extend('valid_date', function($attribute, $value, $parameters)
{
$pieces = explode('/', $value);
if(strpos($value, '/')===FALSE) {
return false;
} else {
if(checkdate($pieces[1], $pieces[0], $pieces[2])) {
return true;
} else {
return false;
}
}
});
Validator::sometimes('dob', 'valid_date', function($input)
{
return apply_regex($input->dob) === true;
});
return [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|unique:users,email',
'dob' => 'sometimes|required|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/|valid_date',
'mobile' => 'required',
'password' => 'required|confirmed'
];
}
But I now get the following error when I submit the form:
FatalErrorException in Facade.php line 216:
Call to undefined method Illuminate\Validation\Factory::sometimes()
There is a documented way to make changes to the request's validator instance in Laravel 5.4. You should implement the withValidator method for that.
Based on the example from #lukasgeiter's answer, you may add the following to your request class:
/**
* Configure the validator instance.
*
* #param \Illuminate\Validation\Validator $validator
* #return void
*/
public function withValidator($validator)
{
$validator->sometimes('dob', 'valid_date', function ($input) {
return apply_regex($input->dob) === true;
});
}
By doing this you don't have to worry about overriding internal methods. Besides, this seems to be the official way for configuring the validator.
You can attach a sometimes() rule by overriding the getValidatorInstance() function in your form request:
protected function getValidatorInstance(){
$validator = parent::getValidatorInstance();
$validator->sometimes('dob', 'valid_date', function($input)
{
return apply_regex($input->dob) === true;
});
return $validator;
}
You just need to add the dob key to the array you are returning, along with the validation ruleset to follow, including sometimes.
In this case:
'dob' : 'sometimes|required|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/|valid_date'
According to your comment
I want the rule valid_date to only run if the regex rule returns true. Otherwise
the valid_date rule errors if the date isnt in the right format.
Validator::extend('valid_date', function($attribute, $value, $parameters)
{
\\use the regex here instead
if (!preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/', $value)) return false;
$pieces = explode('/', $value);
if(strpos($value, '/')===FALSE) {
return false;
} else {
if(checkdate($pieces[1], $pieces[0], $pieces[2])) {
return true;
} else {
return false;
}
}
});
$validator = Validator::make($data, [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|unique:users,email',
'dob' => 'required|valid_date',
'mobile' => 'required',
'password' => 'required|confirmed'
]);

Categories