I have a registration form with 3 select fields (month, day, year) which I want to grab, convert to proper date format, and validate. So, below are the 3 select boxes.
<div class="birthday_selects">
<select name="birthday_month">
....
</select>
<select name="birthday_day">
....
</select>
<select name="birthday_year">
....
</select>
</div>
I've read that I can use the following, but I get the error "...Http\Input not found..."
$birthday = Input::get('birthday_month')."-".Input::get('birthday_day')."-".Input::get('birthday_year')'
Okay, I can dd($birthday) and it comes out in the m-d-y as I've concatenated there. Now, I'm trying to validate that date against a prior date (to see if user is >13 years old). I'm using the following, but no luck.
public function postSignup(Request $request)
{
$birthdate = Input::get('birthday-month')."-".Input::get('birthday-day')."-".Input::get('birthday-year');
$this->validate($request, [
$birthdate => 'required',
]);
dd($birthdate);
}
When just having it be required, it doesn't work.
Your help is appreciated. Thanks!
It's not working because you're doing it wrong. What is wrong with it is the rule.
The rule key must be present in the form field's name.
For your answer, let's assume the $birthday will be 'jul-15-1992'. and you're going to put that variable inside the rule as a key. So, that's not valid because the field will never be present in the form field.
public function formPost(Request $request)
{
$rule = [
'birth_year' => 'required|numeric',
'birth_day' => 'required|numeric',
'birth_month' => 'required|numeric', // may be a string
];
$this->validate($request, $rule);
// check if the day is valid for the month manually.
// then form the DOB now.
}
Create Carbon date and add new [birth_day] input:
$all = $request->all();
$year = $all['birthday_year'];
$month = $all['birthday_month'];
$day = $all['birthday_day'];
// Create Carbon date
$date = Carbon::createFromFormat('Y-m-d', $year.'-'.$month.'-'.$day);
//add new [birth_day] input
$request->request->add(['birth_day' => $date->format('Y-m-d')]);
$validatedData = $request->validate([
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'password' => 'required|string',
'birth_day' => 'required|date_format:Y-m-d|before:today',// validate birth day
]);
Hope this will help you
Related
i made three columns unique together and this is my validation
public function validEdit($request ,$id)
{
$color = $request->get('color');
$size_id = $request->get('size_id');
$name = $request->get('name');
return
Validator::make($request->all(),
[
'name' => 'required|min:4|string|unique:products,name,NULL,id,color,'.$color.',size_id,'.$size_id,',id,'.$id,
'description'=>'required|min:4',
'quantity'=>'required|numeric',
'subcategory_id'=> 'required',
'category_id'=> 'required',
'price'=> 'required|numeric',
'color'=>'required|unique:products,color,NULL,id,name,'.$name.',size_id,'.$size_id,',id,'.$id,
'size_id'=>'required|unique:products,size_id,NULL,id,name,'.$name.',color,'.$color,',id,'.$id,
'images.*'=>'image:png,jpg,jpeg',
// 'photos.*'=>'image:png,jpg,jpeg',
'cover'=> 'image:png,jpg,jpeg'
]);
it works fin , but how i can make it skip validate for the same column
thanks
I have 3 input fields (reservationdate, starttime, endtime) and 1 checkbox (holeday).
If the checkbox clicked I do not need starttime and endtime. On the other hand starttime and endtime is required.
What can I do to solve this task?
I tried in Laravel the required_if validation-function. But I'm certainly using it wrong
// ReservationController store:
$data = $request->validate([
'userid' => 'required|numeric',
'vehicleid' => 'required|numeric',
'budgetid' => 'required|numeric',
'reservationdate' => 'required|date',
'starttime' => 'required_if:holeday,on|date_format:H:i|before_or_equal:endtime',
'endtime' => 'date_format:H:i',
'holeday' => 'boolean'
]);
index.blade.php (only the checkbox)
<div class="input-field col s2">
<label>
<input type="checkbox" name="holeday" class="filled-in" />
<span>Hole Day</span>
</label>
</div>
If the checkbox is checked I get the error-message "The starttime field is required when holeday is on." but in this case I need no error. Hey user it is OK. I donĀ“t need a starttime or endtime. Your clicked the holeday.
Correct me if I am mistaken: you are trying to have starttime and endtime required only when holeday is not checked.
If I recall correctly when a checkbox is unchecked or disabled, it isn't sent at all to the server, so you can verify for the presence/absence of that field name:
// ReservationController store:
$data = $request->validate([
'userid' => 'required|numeric',
'vehicleid' => 'required|numeric',
'budgetid' => 'required|numeric',
'reservationdate' => 'required|date',
'starttime' => 'required_without:holeday|date_format:H:i|before_or_equal:endtime',
'endtime' => 'required_without:holeday|date_format:H:i',
'holeday' => 'required_without_all:starttime,endtime|in:on'
]);
Note: boolean validations accepts only true, 1``and "1" as thruthy values. But if the checkbox is set without a value attribute you would get "on" value as default check value.
However if you pass all of the three fields with their respective values, the validation would still pass, because required doesn't ensure that if the condition is not met that field must not be present.
You can take that into account and just write your logic to check if holeday has "on" value then ignore starttime and endtime values and viceversa.
Otherwise, you have to use a custom validation rule like this one (untested):
Validator::extend('not_present_with', function ($attribute, $value, $parameters, $validator) {
foreach ($parameters as $parameter) {
if (! Arr:has($validator->attributes(), $parameter)) {
return false;
}
}
return ! Arr::has($validator->attributes(), $attribute);
});
Then use the following validation rules:
// ReservationController store:
$data = $request->validate([
'userid' => 'required|numeric',
'vehicleid' => 'required|numeric',
'budgetid' => 'required|numeric',
'reservationdate' => 'required|date',
'starttime' => 'not_present_with:holeday|required_without:holeday|date_format:H:i|before_or_equal:endtime',
'endtime' => 'not_present_with:holeday|required_without:holeday|date_format:H:i',
'holeday' => 'not_present_with:starttime,endtime|required_without_all:starttime,endtime|in:on'
]);
i'm trying to make inquiry form where costumer fill up form then check the value on the checkbox then once they submit form will send email to me listing all the information the customer selected, now problem is i want to change this[event_id,requirement_id] instead of id replace it with name those two id parameter is from my two model listed below.
Model:
Event:[id,name]
Requirement:[id,name]
Controller:
public function store(Request $request)
{
$summary=[
'name' => $request->fullname,
'email' => $request->email,
'company' => $request->company,
'event' => $request->event_id,
'requirement' => $request->requirement_id
];
return $summary;
Mail::send('emails.contact-message',[
],function($mail) use($summary){
$mail->from('myemail#gmail.com', 'tester');
$mail->to('myemail#gmail.com')->subject('Contact Message');
});
return redirect()->back();
}
This is the result of my return request:
{"name":"myname","email":"myemail#gmail.com","company":"mycompany","event":["1","2"],"requirement":["1","2"]}
As you can see the array Event has value of 1 and 2 i wanted to replace it with its name output should be [Wedding,Birthday] i'm sorry for my bad english hope you understand me..
Well, you'd need to pull the name from your models.
The following should do the trick:
$events = App\Event::whereIn('id', $request->event_id)
->get()
->pluck('name')
->toArray();
$requirements = App\Requirement::whereIn('id', $request->requirement_id)
->get()
->pluck('name')
->toArray();
Obviously, replace name in the above example with the actual name field in your models. This is just an example.
$events and $requirements will both be an array containing the names matching the ids you are supplying in your request.
You also need to change your $summary array as follows:
$summary = [
'name' => $request->fullname,
'email' => $request->email,
'company' => $request->company,
'event' => $events
'requirement' => $requirements
];
this is my doctors id
this is my user id
I can't insert anything when I put unique in the validation of doctor_id. When there's no unique it works fine.
I want to avoid duplicate doctor_id where auth::id == 1. Any advice is appreciated.
public function store(Request $request, $id)
{
$auth = Auth::id();
$constraints = [
'doctor_id' => 'required|unique',
'day1' => 'required|max:20',
'day2'=> 'required|max:60',
'day3' => 'required|max:60'
];
$input = [
'users_id' => $auth,
'doctor_id' => $id,
'day1' => $request['day1'],
'day2' => $request['day2'],
'day3' => $request['day3'],
'day4' => $request['day4'],
...
'day27' => $request['day27'],
'status' => '1'
];
$this->validate($request, $constraints);
Itinerary::create($input);
$added = array('added'=> 'Added Doctor to Itinerary Successful!!');
return redirect()->back()->with($added);
Have you tried this? (assuming your table is named itineraries):
'doctor_id' => 'unique:itineraries'
According to Laravel Doc, you should add the table name, and column name if possible:
unique:table,column,except,idColumn
How can I check the age of a user upon registration? I want to set the minimum age to be 13 years old. I ask for the user's date of birth and when I validate the other credentials, I want to check that they are in fact 13+.
An excerpt from my User model looks like this:
$rules = [
'name' => 'required|alpha|min:1',
'email' => 'required|email|unique:users',
'dob' => 'required|date'
];
How can I check that the date entered is 13 years ago or more?
I have seen that I can use the before:yyy-mm-dd rule from the Laravel Docs, like so:
$rules = [
'name' => 'required|alpha|min:1',
'email' => 'required|email|unique:users',
'dob' => 'required|date|before:2001-04-15'
];
How do I calculate the value?
How do I use that value within the rules?
A simple way to check that the date is greater(older) than N years is to set the before rule to minus N years.
$rules = [
'dob' => 'required|date|before:-13 years',
]
You can use Carbon which comes with laravel
$dt = new Carbon\Carbon();
$before = $dt->subYears(13)->format('Y-m-d');
$rules = [
...
'dob' => 'required|date|before:' . $before
];
RMcLeod answer is OK, but I'd suggest you extracting this as a custom rule:
Validator::extend('olderThan', function($attribute, $value, $parameters)
{
$minAge = ( ! empty($parameters)) ? (int) $parameters[0] : 13;
return (new DateTime)->diff(new DateTime($value))->y >= $minAge;
// or the same using Carbon:
// return Carbon\Carbon::now()->diff(new Carbon\Carbon($value))->y >= $minAge;
});
This way you can use the rule for any age you like:
$rules = ['dob' => 'olderThan']; // checks for 13 years as a default age
$rules = ['dob' => 'olderThan:15']; // checks for 15 years etc
This is a bit old, but I want to share the way I do it.
public function rules()
{
return
[
...
'age' => 'required|date|before_or_equal:'.\Carbon\Carbon::now()->subYears(18)->format('Y-m-d'),
...
];
}
Using before is nice but it's a bit ugly for the end user, because if today it's his birthday, he won't be able to pass. With before_or_equal you get the perfect behaviour. A way to improve this would be checking the timezone with Carbon if you target a worldwide audience.
I have Implement This & Successfully Work
You Can Use
before:'.now()->subYears(18)->toDateString()
$validator = Validator::make($request->all(), [
'birthdate' => 'required|date|before:'.now()->subYears(18)->toDateString(),
], [
'birthdate.before' => trans('18 year validation'),
]);