Laravel validation rule "URL" does not pass empty value - laravel

I have input text field whith validation rule :
public function rules()
{
return [
'field' => 'url',
];
}
It`s not required (field can be empty) but validation return an error.

Solve problem use "nullable":
public function rules()
{
return [
'field' => 'nullable|url',
];
}

Add sometimes rule to the validation. This means to validate it for a URL when the value is not given or null.
public function rules()
{
return [
'field' => 'sometimes|url',
];
}

when we submmitting values from js, like through a FormData, the value null could be submitted as string containing 'null', which may pass a nullable rule, cause further type check fail. so be sure to make this value be posted as '', literaly nothing, no a 'null' string

When you use formData (js) to submit your request, "null" is assigned by default for all empty fields. This will pass through Laravel "nullable" validaton and indicate as invalid input. So, please, use something like below in your validation rules.
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$rules = [];
if($this->filled('field') && $this->field!= 'null'){
$rules['field'] = 'url';
}
return $rules;
}
In order to do this use laravel's form requests. https://laravel.com/docs/8.x/validation#creating-form-requests

Related

Laravel validation rules - how optional, but only if another condition is true

How can I make a custom rule using Validation, so that the field can be nullable 'since' call function the result is true, otherwise, the field becomes required.
Of course I tried to use the 'nullable', but even if the field is empty, the Validation should execute the checkAreasDiff() function to validate that the field can be empty during the update.
In my controller, I created a function:
private function validator_update(array $data) {
\Validator::extend('areas_diff', function($attribute, $value, $parameters, $validator) {
return checkAreasDiff();
}, 'VALIDATOR AREAS_DIFF OK.');
/**
* RULES
*/
$rules = [
'fiscalizoarea' => 'areas_diff',
];
/**
* Return \Validator
*/
return \Validator::make($data, $rules, $msgs);
}
If I understand the question correctly, you want one field to be required only if another is not null?
There is a Laravel rule for that already: required_with.
required_with:foo,bar,...
The field under validation must be present and not empty only if any
of the other specified fields are present.
Or, if I'm getting your logic back to front: required_without
required_without:foo,bar,...
The field under validation must be present and not empty only when any
of the other specified fields are not present.

Laravel Custom validation rule with parameters

I have write this function rule in CustomRequest to check checkHackInputUser rule that define in provider:
Actually i want to check the value that pass in route
for example :
http://www.somedomain.com/user/{id}
I what do some operation on this $id variable
with my checkHackInputUser rule
Here is CustomRequest:
public function rules()
{
$request_id = $this->route('user');
$rules = [];
if($this->method() == "DELETE" || $this->method() == "GET" )
$rules = [
'role_list' => 'required|checkHackInputUser:'.$request_id,
];
return $rules;
}
The problem is,this rule(checkHackInputUser) doesn't work if i remove required role.
Here is the checkHackInputUser validation function in provider:
public function boot()
{
$this->app['validator']->extend('checkHackInputUser',function($attr,$value,$params){
//Some validation
return false or true;
});
}
You can conditionally validate input when present using sometimes.
In some situations, 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:
$v = Validator::make($data, [
'email' => 'sometimes|required|email',
]);

Yii2 validation. Run filter on multiple attributes

public function rules()
{
return [
[['option_list', 'modifier'], 'filter', 'filter' => function($value) {
// I can get the value but I don't know to which attribute it belongs (option_list or modifier)
}],
];
}
How do I get an attribute name which is being processed? The only workaround that I found is to make separate filter for each attribute...
The first parameter passed to validation function is $attribute so You can use it as follows
public function rules()
{
return [
[['option_list', 'modifier'], function($attribute) {
// use $this->$attribute for conditions or filtering
// use $this->addError($attribute, '<error message>') for adding errors
}],
];
}
see http://www.yiiframework.com/doc-2.0/guide-input-validation.html#creating-validators

Laravel Validation sometimes rules for date validation

I currently have a validation rule which looks like this:
public function rules()
{
return [
'startDate' => 'required|sometimes|before_or_equal:endDate',
'endDate' => 'sometimes|required|after_or_equal:startDate',
];
}
The sometimes option works as I understand it on the basis that if the field is present, run the validation rule. However, if the end date is not sent or is null, my before or equal rule kicks in and fails. In some instances within my application, end date will be null. Is there a way to 'cancel' the startDate validation rule in this instance or would I need to create a custom validator for this purpose?
something like before_or_equal_when_present ?
You can use IFs to add and manipulate rules in the rules function. You can access the inputs there referring to $this as the request itself:
public function rules()
{
$rules = [
'startDate' => 'required|sometimes|before_or_equal:endDate',
'endDate' => 'sometimes|required|after_or_equal:startDate',
];
if( $this->input('endDate') > 0)
$rules['endDate'] = "rule". $rules['endDate']
return $rules;
}
This is just a mockup just to let you know that you can manipulate and have access to the fields passed.

Validate only if the field is entered in Laravel 5.2

public function rules() {
return [
'num_of_devices' => 'integer'
];
}
I want to validate an integer field if and only if the field is entered. But above rule validates it for integer even if the field is empty. I used somtimes, but no result. But when I var_dump($num_of_devices) it is string.I am using Laravel 5.2. I think It was working fine in 5.1.
From version 5.3 you can use nullable
public function rules() {
return [
'num_of_devices' => 'nullable | integer'
];
}
Add a rule to array, if input is not empty. You could collect all your validation rules to $rules array and then return the array.
if( !empty(Input::get('num_of_devices')) ){
$rules['num_of_devices'] = 'integer';
}
You’re looking for the sometimes validation rule. From the documentation:
…run validation checks against a field only if that field is present in the input array.

Resources