How to check for null in other field laravel FormRequest? - laravel

I am trying to exclude a field from being validated when another field has a value (not null). In this case, i want 'deleted_pictures' to be excluded if product_pictures is an array (not null). My problem is that i think exclude_unless:product_pictures,null evaluates null as a string, and not as, well, null.
This is my rule.
ProductRequest.php
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,null', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless product_pictures has a value of 'null'
I tried this to confirm my suspicion and it works like it should.
//test_field = 'some_value'
return [
'test_field' => 'required|string'
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:test_field,some_value', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless test_field has a value of 'some_value'
In my first case, deleted_pictures is excluded because it doesn't detect that product_pictures is 'null' (string)
While on the second case, deleted_pictures is NOT excluded because test_field matches the given value.
My question is, how do you evaluate null value in FormRequest Laravel?

So apparently you can just leave the second parameter blank to evaluate it as null
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,', 'required', new cant_delete_all($max,'1')],
];
I'm not sure if this is how its supposed to be done or intended behavior. But im just gonna leave this answer just in case someone might need it. If someone can suggest the 'proper' way of doing it then I'll accept that instead.

Related

Row inserted but number not

I trying to insert a row in codeigniter and row inserted.
Problem is all row inserted properly but in sql bighint(11) field inserted 0.
I checked properly in array value given.
$data = [
'sku' => $POST['rec1'],
'pruch_price' => $POST['rec2'],
'sell_price' => $POST['rec3']
];
$model->insert ($data);
you should use $_POST[] instead of $POST.
But better yet, don't send $_POST directly to the models, instead use the post request provided by Codeigniter 4.
$data = [
'sku' => $this->request->getPost('rec1'),
'pruch_price' => $this->request->getPost('rec2'),
'sell_price' => $this->request->getPost('rec3')
];
$model->insert($data);

Laravel updateOrInsert with 'OR' or 'AND' operator in first arguments?

While it is possible to have multiple arguments for the updateOrInsert in Laravel query builder and what is the operator used by default.
For example in the documentation it is mentioned:
DB::table('users')
->updateOrInsert(
['email' => 'john#example.com', 'name' => 'John'],
['votes' => '2']
);
Does that mean that email && name are checked or does it mean email || name is checked? How can we control it for one or the other if required?
Please forgive me if this is a silly question or if it is not worded as per the correct vocabulary, as I am new to Laravel. I couldn't find this information in the documentation or API.
updateOrInsert() method is used to update an existing record in the database if matching the condition or create if no matching record exists. Its return type is Boolean.
Syntax :
DB::table('blogs')->updateOrInsert(
[Conditions],
[fields with value]
);
In your query :
DB::table('users')->updateOrInsert(
['email' => 'john#example.com', 'name' => 'John'],
['votes' => '2']
);
It will check if email == 'john#example.com' & name == 'john', then it will update votes=2.

input validation for number between 0 and 100?

I have to validate an integer which is not required or necessary but if it is entered, it must between 0 and 100
Actually I have to enter score value ranging 0 to 100
I tried 'digits_between:1,2' but value 0 and 100 cannot be entered
any help?
from Laravel 5.6 docs, validation rule between & rule nullable
$request->validate([
'field_name' => 'nullable|integer|between:0,100',
]);
if you want to accept float values as well, use rule numeric
$request->validate([
'field_name' => 'nullable|numeric|between:0,100',
]);
You can try this (docs).
[
'number' => 'sometimes|integer|between:0,100'
]
You can just use validation for condition without required
"field_name" => 'numeric|between:0,99.99',
This is the full line of code
$v = Validator::make($data, ['field' => 'numeric|between:0,99.99']);

Laravel Validator

I have the following rule in the validator:
'keywords' => 'array|required'
'date_intervals' => 'array|required'
The array needs to be populated with at minimum 1 element (should not be empty).
Is there an existing rule to achieve this, or is it required that I write a custom rule for it?
Does min:1 work with array validation?
Thanks.
No, you'd be better counting the array elements and then passing the count for validation.
$count = count(Input::get('keywords'));
$input = ['keywords' => Input::get('keywords'),'count' => $count];
$rules = ['keywords' => 'required|array', 'count' => 'min:1'];

sfValidatorRegex not working

I am trying validate a username field on a register form so that a username can only be made up of letters. However, when I use the sfValidatorRegex(), it always returns invalid ("johnny" will return invalid, as does "JoHnNy"). Here's the code I'm using:
// From RegisterForm.class.php
$this->validatorSchema['username'] = new sfValidatorRegex(
array(
'pattern' => '[A-Za-z]',
),
array(
'invalid' => 'only lowercase letters',
)
);
What am I missing?
Didn't understand whether you wanted lower or uppercase but try:
Only lowercase: '/^[a-z]*$/'
Lower or uppercase: '/^[A-z]*$/i'

Resources