So, this is what i've tried already.
$regex = '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/';
$rules = [
'nextcontactSchool' => 'nullable|digits_between:7,10',
'websiteSchool' => 'nullable|regex:' . $regex,
];
I did this in a controller once i figured that i can't properly make use of a rule. But the problem here is that, I cant seem to pass the empty data to the database and everytime that i do that, i get a error message.
Can you please help me out?
You can use nullable and it will pass if websiteSchool and nextcontactSchool are not present
$request->validate([
'websiteSchool' => ['nullable', 'url'],
'nextcontactSchool' => ['nullable', 'digits_between:7,10']
]);
If this is not what you need, consider the following validation methods:
required_if
present
so, i came to know the answer behind it. when using nullable it sends a null value for sure but it send's the nullable value in terms of a string. so rather than comparing it to null, we would be comparing it to 'null'.
e.g:
if($variable!='null'){
//do something
}
Related
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.
Using Laravel 5.4, my users have an auto-complete helper to put values into an input. I want to validate that each of the values exists in the database when inserting.
Inputted value for "unit" : "12,13,14"
How do I check that, unit "12" and unit "13" and unit "14" exist in the database before doing the insert?
$units = array_filter(array_unique(explode(",", $request->unit)));
// input "12,13,14" becomes [12,13,14]
$this->validate($request,[
'unit' => 'required|exists:units.id,'.$units,
]);
Do I have to use a custom validation rule, or does laravel have something handy like 'required|existsAllValuesInThisArray' sorta thing? Haven't found anything in documentation about it.
I also found this, but it's for like multiple select fields or checkboxes sorta thing from the looks of it.
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);
Update : I ended up using javascript to split the input into an array before sending it off for processing. So my input name became "units[]" instead of "units"
Try the following:
$this->validate($request,[ 'unit.*' => 'required|exists:units.id,'.$units, ]);
Since $units is an array, the rule unit.* should check for each element of the array.
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';
The docs say that 'notBlank' is a validation rule for fields that you want to make sure they are not empty, as in !empty($somevalue), but when I leave the field blank ('') or when I put a value in the field ('s0meCraZyPasSworD') it still display the error message?
Can anyone see what I'm doing wrong? The rest of the validations work like minlength, but I commented them out to get a better idea of why 'notBlank' doesn't appear to be working...
CONTROLLER:
// Set of validation rules to be run
$validateRules = [
'fieldList' => [
'currentpassword',
'newpassword',
'confirmpassword'
]
];
if ($this->Admin->validates( $validateRules )) {
...
}
MODEL:
class Admin extends AppModel
{
public $name = 'Admin';
public $validate = [
'currentpassword' => [
'notBlank' => [
'rule' => 'notBlank',
'message' => 'Current password is required.'
]
],
...
You tagged CakePHP 2.4 - notBlank was added in 2.7 so you have to use notEmpty or set allowEmpty to false...
The data sent to the model’s save() method must contain data for the
login field. If it doesn’t, validation will fail. The default value
for this key is boolean false.
required => true does not mean the same as the validation rule
notBlank(). required => true indicates that the array key must be
present - it does not mean it must have a value
From the official website:
http://book.cakephp.org/2.0/en/models/data-validation.html#required
So you will need another rule like the allowEmpty to validate this field and not the notBlank rule.