Validate Age Variable - Laravel RegisterController - laravel

I have added 3 fields to the Laravel OOB Registration Form, they are Birth Month, Day, and Year. I pass these fields to the validator function in the RegisterController and convert them to an age with Carbon:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
This part works fine, I can pass the variable to a field in the table and see the correct age.
How do I add $theAge to my Validator?
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
]);
I have tried the following but it appears to be ignored on validation:
$theAge => 'bail|min:21'
I have looked into the After Validation Hook but don't understand how to use it in my situation.

you can add the $theAge variable to the data array.
$data['age'] = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;

You can put the calculated value back in the $data before you call the validator like this:
$theAge = Carbon::createFromDate($data['birthyear'], $data['birthmonth'], $data['birthday'])->age;
$data['age'] = $theAge;
return Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:2',
'zipcode' => 'required|string|max:10',
'brand' => 'required',
'opt_in' => 'required',
'g-recaptcha-response' => 'required|captcha',
'birthmonth' => 'required',
'birthday' => 'required',
'birthyear' => 'required',
'age' => 'min:21'
]);
Alternatively, you can let the user select their date of birth using date picker (e.g. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date) like this:
<input name="birthday" type="date">
and in your validator, do this define the age check plus a custom error message:
return Validator::make($data, [
// ... snipped
'birthday' => 'required|date|before_or_equal:' . Carbon::now()->subYears(21)->toDateString()
], [
'birthday.before_or_equal' => 'You must be at least 21 years old.'
]);

Related

parse_url() expects parameter 1 to be string, array given when creating user laravel

Im building my onw registration in laravel and when im trying to hash my password i get the error parse_url() expects parameter 1 to be string, array given
//Controller
HomeController.php
$filteredValidation = $request->except('_token');
$password = Hash::make($filteredValidation['password']);
UserRegistrationRequest::create([
'firstname' => $filteredValidation['firstname'],
'lastname' => $filteredValidation['lastname'],
'email' => $filteredValidation['email'],
'year' => $filteredValidation['year'],
'avatar' => $filteredValidation['firstname'],
'buddy' => $filteredValidation['firstname'],
'password' => $password,
]);
//request
UserRegistrationRequest.php
public function rules()
{
return [
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required',
'year' => 'required',
'password' => 'required',
];
}
I have no idea why this is happening

Is there anyway group the errors of multiple form validations in laravel

In the given scenario
request()->validate([
'type' => 'required',
'category' => 'required'
]);
and Again
request()->validate([
'name' => 'required',
'gender' => 'required
]);
Is it possible to get some sort of centralized or complied error that encompasses both the validations?
Then you should use Validator facade to handle this kind of cases.
for ex.
$validator = Validator::make($request->only('type', 'category), [
'type' => 'required',
'category' => 'required'
]);
$validator2 = Validator::make($request->only('name', 'gender'), [
'name' => 'required',
'gender' => 'required'
]);
if ($validator->fails() || $validator2->fails()) {
// return merge $validator->errors() and $validator2->errors();
}

Laravel - syntax error, unexpected ''phone'' (T_CONSTANT_ENCAPSED_STRING), expecting ']'

I am using Laravel-5.8 as backend for an application. I have written all the Api for the endpoints.
Laravel: ApiController
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
//'email' => 'required|email|unique:users|max:255',
'phone' => 'required|max:14',
'business_name' => 'required',
'truck_type' => 'required',
'truck_required' => 'required',
'quote_origin' => 'required',
'quote_destination' => 'required',
'commodity' => 'required',
// 'weight' => 'required',
'loading_date' => 'required'
]);
$clientquote = new ClientQuote([
'first_name' => $request->first_name,
'last_name'=> $request->last_name,
'email' => $request->email
'phone' => $request->phone,
'business_name' => $request->business_name,
'address' => $request->address,
'comment' => $request->comment,
'truck_type' => $request->truck_type,
'truck_required' => $request->truck_required,
'quote_origin' => $request->quote_origin,
'quote_destination' => $request->quote_destination,
'commodity' => $request->commodity,
'loading_date' => $request->loading_date
]);
$clientquote->save();
$mainData = array();
$mainData['to'] = $clientquote->toArray()['email'];
$mainData['from'] = "support#tsllimited.com";
$mainData['subject'] = "Client Quote";
$mainData['content'] = "Your Quote have been successfully received. You will hear from us shortly through the provided email. Thank you!";
$this->mailSend($mainData);
return response()->json([
'message' => 'Quote Successfully Sent!'
], 201);
}
public function indexClientQuote(Request $request) {
return response()->json(ClientQuote::get());
}
When I tested the Request on the POSTMAN, I got the error shown below:
I don't know why it's expecting ']'.
What could have caused the error?
you missed ',' after email
'email' => $request->email
'phone' => $request->phone,
To
$clientquote = new ClientQuote([
'first_name' => $request->first_name,
'last_name'=> $request->last_name,
'email' => $request->email, //here you missed comma
'phone' => $request->phone,
'business_name' => $request->business_name,
'address' => $request->address,
'comment' => $request->comment,
'truck_type' => $request->truck_type,
'truck_required' => $request->truck_required,
'quote_origin' => $request->quote_origin,
'quote_destination' => $request->quote_destination,
'commodity' => $request->commodity,
'loading_date' => $request->loading_date
]);

How to select data from multiple dropdown and combined them into one variable in laravel?

I have three dropdowns for Day, Month, and year for Date of Birth Data and one column dob in the database. I want to convert selected data into the date of birth format then stored in the database. this is creating syntax error.
my controller code :
protected function create(array $data)
{
'month' => $data['month'],
'day' => $data['day'],
'year' => $data['year'],
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'gender' =>$data['gender'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => $data['phone'],
'dob' => "month/day/year",
]);
}
$dob = $data['year'].'-'.$data['month'].'-'.$data['day'];
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'gender' =>$data['gender'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => $data['phone'],
'dob' => $dob,
]);

Laravel - Validate value against string

I am running some basic validation inside a Laravel 5.5 controller like this...
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'required',
]);
Is there a way to check if 'mykey' matches a php string I have saved? I know I can do an if statement and compare them but wondered if there was a way I could do this inside the validation itself?
You can use in rule, This works for n number of values
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => [
Rule::in([env('MY_KEY'),config('app.another_key')]),
]
]);
Laravel provides a regex option for validation. Depending on the complexity of the string comparison it may be useful:
https://laravel.com/docs/5.5/validation#rule-regex
You can this rule:
$key = "my_saved_key"
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'in:' . $key,
]
]);

Resources