How can I validate my inputs from a GET method?
Example URL: localhost:8000?salary=2000&name=sample&description=vowewljfodigjfdglfd
In the URL I have 3 inputs and I want to validate:
Salary - should accept only numeric
Name - should accept only alphabetic
Description - should accept with max:1000
Somebody knows how to do this?
The Laravel validator doesn't care where the data came from. You can manually create a validator and pass it the query string data.
$validator = Validator::make($request->query(), [
'salary' => 'numeric',
'name' => 'alpha_num',
'description' => 'max:1000',
]);
if ($validator->fails()) {
// show an error
}
Side note: As someone with a hyphenated last name, I implore you not to treat name as alphanumeric. See Falsehoods Programmers Believe About Names.
Related
If you create a validation rule in Laravel, but leave out |required and just use, say the email rule. What does it default to?
For example
$request->validate([
'email' => 'email'
]);
Are these validation rules equal?
$request->validate([
'email' => 'sometimes|email'
]);
// vs
$request->validate([
'email' => 'email'
]);
Apologies if this has been answered before. I tried my very best to search for similar questions, but I may have used the wrong words (not sure how to phrase this for a Google/Stack Overflow search).
$request->validate([
'email' => 'email'
]);
This will always validate the email key, even if empty, to be a valid email format.
$request->validate([
'email' => 'sometimes|email'
]);
This will only validate the email key if the key is present in $request->all().
So the difference is that with sometimes you'll only validate it if the $request object contains it, whilst otherwise it would always validate against the key.
If I could simplify it, I would say sometimes means, only apply the rest of the validation rules if the field shows up in the request. Imagine sometimes is like an if statement that checks if the field is present in the request/input before applying any of the rules.
It can be a tedious thing to wrap ones head around, but here is some examples:
input: []
rules: ['email' => 'sometimes|email']
result: pass, the request is empty so sometimes won't apply any of the rules
input: ['email' => '1']
rules: ['email' => 'sometimes|email']
result: fail, the field is present though invalid email so the email rule fails!
input: []
rules: ['email' => 'email']
result: fail, the request is empty so email is invalid!
I personally have never used the sometimes rule once. According to the docs:
https://laravel.com/docs/7.x/validation#conditionally-adding-rules
you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list
If I understand correctly, this largely depends on your frontend implementation. If you are using regular html <form> elements where you have an input with name="email", it will send a key named email as soon as you submit the form.
So in order to NOT send such key, you have to remove it from the form, or manually make a HTTP request (so disregarding any HTML form) such that you can decide your own input fields.
public function store()
{
$this->validate(request(),[
// Third try
‘username’ => ‘required|string|regex:/\w*$/|max:255|unique:users’,
// Second try
‘username’ => ‘required|string|regex:/^[A-Za-z0-9]+(?:[_-][A-Za-z0-9]+)*$/|max:255|unique:users‘,
// First try
’username’ => ‘required|string|max:255’,
]);
}
The username field was working well with numbers included (at first try) but then I forgot to include “unique:users”, then the form started rejecting it (redirects back with username field underlined with wriggle red line). Plus I have used laravel’s “alpha_dash” several times but keeps rejecting the input. My aim is mixture of letters, numbers, underscores and dashes.
Some please help me make this right. Thanks
You're missing parameters. It should be unique:table,column,except,primarykeyname
The last two parameters are optional. In your case, your validation for storing an user should look like this:
'username' => 'required|string|regex:/\w*$/|max:255|unique:users,username',
That way, you're saying there should not be an user with that same username in the database.
When you want to update an user however, you should add the user's id as the third parameter so the validation for unique username ignores the user you're trying to update.
'username' => 'required|string|regex:/\w*$/|max:255|unique:users,username,'.$user->id,
If your primary key is not named id, you add its name as the fourth parameter:
'username' => 'required|string|regex:/\w*$/|max:255|unique:users,username,'.$user->userId.',userId',
When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character :
public function store(Request $request)
{
$validatedData = $request->validate([
'username' =>
array(
'required',
'unique:users,username',
'max:255',
'regex:/\w*$/'
)
]);
}
I created a form with the following fields:
Name
Email
Country
City
Address
If the user selects a country that has states (ex. United States) then the form transforms to:
Name
Email
Country
State
City
Address
To validate this I created a separate form request like so:
public function rules()
{
return [
'name' => 'required|max:255',
'email' => 'required|email,
'country_id' => 'required|integer',
'state_id' => 'nullable|integer',
'city_id' => 'required|integer',
'address' => 'required',
];
}
The problem is that if I leave it like that, then if I don't select a state it will pass validation.
If i make it:
'state_id' => 'sometimes|nullable|integer',
Then again it passes validation.
If I make it:
'state_id' => 'required|nullable|integer',
It will not pass validation, but then again it will throw a validation error if there is no state field in the form.
I read a lot of articles about this but nothing seems to solve it for me.
PS1: I want to solve this in the form request, not in the controller. I assume that an
if($request->has('states')){...}
can help, but then again, i would like to keep everything tidy in the form request.
PS2: I am using VueJS and Axios to add/remove states from the form. The whole form is actually a Vue component.
Any clues?
Thank you in advance!
You can conditionally add rules via the sometimes method on Validator.
$v->sometimes('state_id', 'required|integer', function ($input) {
return in_array($input->countries, [1,2,3,4...]
});
You could use the required_with line of parameters, but because the validation is based on the value of the input instead of just the presence, the custom validation rule is probably your best bet.
Per https://laravel.com/docs/5.7/validation#conditionally-adding-rules
Since upgrading laravel from 5.1 to 5.3, I've got couple of odd issues with Validation.
When I post a data like this:
firstName null
And the validation rules are like this:
$validator = Validator::make($postData, [
'firstName' => 'string|max:255',
'lastName' => 'string|max:255'
]);
The above fails with the messages something like "The XYZ must be a string.". What I don't understand is:
Why is the validation failing when it is not set as required?
Meaning, it should ignore it and not throw an error if the value is
empty, right?
Why does the validation fail if the value is set as null?
Why does the validation fail when the parameter is not sent at all?
(like the lastName which is not posted at all)
Has something changed in Laravel 5.3 validations?
Add nullable rule:
'firstName' => 'string|max:255|nullable',
'lastName' => 'string|max:255|nullable'
The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.
When you want something to be required but the value itself can be empty, like an empty string.
Validator::make($postData, [
'firstName' => 'present|string|max:255|nullable',
'lastName' => 'present|string|max:255|nullable'
]);
Useful in scenarios like "notes", which can be emptied by removing the input field from all its text and hit save.
I am trying to ensure a field is valid if the value appears in a predefined array, but it's not working for me.
The validation rule I am using is:
'title' => [
'required',
'in' => ['Mr', 'Mrs', 'Miss', 'Ms'],
],
But it seems to pass validation if I enter an invalid value, such as "Dr".
Anyone know the correct way to do this?
Try with a string validation rule instead of array:
'title' => 'required|in:Mr,Mrs,Miss,Ms';