is there anyway to validate incoming unixtime's parameter to server and compare that second one is greather that first one by laravel validation?
my code only validate date format
$this->validate($request, [
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
]
A timestamp is a number and can be anything between 0 and 2147483647. That's all you can validate. (And of course that one is bigger than the other.)
So, you could do the following:
$this->validate($request, [
'start_date' => 'required|numeric',
'end_date' => 'required|numeric|gte:start_date',
]);
numeric will make sure the input is a number
gte checks if the value is greater than or equal to another number.
Related
This question already has an answer here:
laravel unique name says already taken if you edit/update that particular object
(1 answer)
Closed 1 year ago.
I have an edit profile form where the inputs have values assigned by the user in the past. The problem I'm facing is that when the user does not change these values the validation mechanism of laravel throughs an error, e.g if username:Gass is not changed then the user clicks on submit and it's faced with the error: The username has already been taken.
How can I skip the validation if the input value has not been edited?
My validation
$request->validate([
'username' => ['nullable','max:15','unique:users', 'regex:/^[a-zA-Z0-9_-]+$/'],
'email' => ['nullable','string', 'email', 'max:50', 'unique:users'],
'about' => ['nullable','max:300'],
'location' => ['nullable','max:30','regex:/^[a-zA-Z0-9., ]+$/'],
'website' => ['nullable','max:40','regex:/^((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)$/'],
'avatar' => ['nullable','image','mimes:jpg,png,jpeg,gif','max:500','dimensions:min_width=100,min_height=100'],
'twitter' => ['nullable','max:15', 'regex:/^[a-zA-Z0-9_]+$/'],
'youtube' => ['nullable','max:50', 'regex:/^[a-zA-Z0-9_-]+$/'],
]);
You can force a Unique rule to ignore a given ID. See the Laravel documentation. Scroll to section: Forcing A Unique Rule To Ignore A Given ID.
In your example, something like this should suffice:
$request->validate([
...,
'username' => ['nullable','max:15',Rule::unique('users')->ignore(auth()->id()), 'regex:/^[a-zA-Z0-9_-]+$/'],
]);
the ignore() method accepts 2 parameters, the first being the value to check for, the second parameter accepts the column of the table to check, but defaults to id.
Assuming your user is logged in here.
in update, you must ignore that item for unique validation rule like
'username' => ['nullable','max:15',Rule::unique('users')->ignore($this->id, 'id'), 'regex:/^[a-zA-Z0-9_-]+$/'],
I want to validate the form data if it is just numeric/integer (as in just numbers). Based on Laravel's documentation there are two specific validators for that. But the problem I'm facing is that both the validators accept non-numeric characters such as "+" or "-".
numeric
The field under validation must have a numeric value.
integer
The field under validation must be numeric.
How can I make the validation to only accept numbers and not other non-numeric characters?
'main_telephone' => 'numeric',
'main_fax' => 'integer',
'direct_telephone' => 'integer',
'mobile' => 'integer',
Below is the screenshot
if anyone has come across this problem the best possible solution is to use regex. I do not know why I didn't think of this before.
'main_telephone' => 'integer|regex:/^[0-9]*$/',
'main_fax' => 'integer|regex:/^[0-9]*$/',
'direct_telephone' => 'integer|regex:/^[0-9]*$/',
'mobile' => 'integer|regex:/^[0-9]*$/',
I would like to create a validation rule which checks if the combination of two values is unique.
There is a field for the street (id) and the house number. Both fields are required. Further, no new entry should be created if a certain combination of street and house number already exists.
How can I achieve this with Laravel?
What I have so far is only this:
protected $rules = [
'street_id' => 'required',
'tree_number' => 'required',
];
I guess this would be possible by using Rule Objects. Then I would query the DB if a certain combination is already stored. But can this be done in a simpler way as well?
Something like below
'street_id' => ['required', 'unique:table,street_id,'.$request->input('street_id').',NULL,id,tree_number,'.$request->input('tree_number')]
I am working on a web app and I needed to validate a form. The form has two numeric fields and I need to make sure one is greater than or equal to the other.
So I used these rules.
[
'adults' => ['required', 'numeric'],
'people' => ['required', 'numeric', 'gte:adults'],
]
Everything else is working except in one case. Say adults is missing or null but people is not. Then I would expect adults is required message. But instead I get
InvalidArgumentException: The values under comparison must be of the same type.
So it seems gte rules is comparing types of the two fields even when one is null with a required rule. How can I get around this?
I think it is a problem with the order the rules are evaluated.
If adults is evaluated first, It would fail with adults is required error. However, in case people is evaluated first, it tries to compare it to adults, throwing the error. You could solve this by conditionally adding the last rule:
[
'adults' => ['required', 'numeric'],
'people' => ['required', 'numeric', $request->has('adults') ? 'gte:adults' : ''],
]
Suppose, in the feedback form we want to ask at least one of two contacts: email and/or telephone number. So, in the validation rules will be required_without for each other:
$this->validate($request, [
'email' => 'required_without:tel|email',
'tel' => 'required_without:email|regex:/(01)[0-9]{9}/'
], $messages);
We need to validate the contact data format only when it has been inputted, but in the code above the format of both fields will be checked. Because we need at least one contact, sometimes rule is no use.