Input required only if specific option is selected - laravel

I have a form that contains a select and an input field. To simplify the example, let's say the three options of the select are 1, 2, and 3.
I need to create a set of validation rules where input is only required if option 2 is selected. How can I create this rule?
Here is my set of rules so far:
$rules = array(
'selectField' => 'required',
'inputField' => '', // only required if option 2 is selected
);

You could use the required_if validation rule?
https://laravel.com/docs/5.1/validation#rule-required-if
$rules = [
'selectField' => 'required',
'inputField' => 'required_if:selectField,2',
];

Related

Laravel validation in combination of two array data

How could I validate it from request file?
I want to check distinct combining floor_id and material_id,
having a floor id can not have same material_id twice or more
From a form data i get this kind of output
project_id => 5,
date => 12-15-2022,
floor_id => array(
0=> 1,
1=> 5,
2=> 12
),
material_id => array(
0=>15,
1=>2,
2=>20
),
quantity => array(
0=>2500,
1=>3500,
2=>6522
)
How could I validate it from request file
may be you can use validation distinct
https://laravel.com/docs/9.x/validation#rule-distinct
You can write a custom validation rule to serve your purpose.
Here you will get some idea https://laravel.com/docs/9.x/validation#custom-validation-rules

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);

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'];

Magento both AND and OR conditions in a collection

I want make a query which looks like below to filter some products (using attributes) from product collection.
SELECT <attributes>
FROM <tables & joins>
WHERE (<some AND conditions>) OR (<some AND conditions>)
WHERE condition should filter products that match either first set of AND conditions or second set of AND conditions.
Problem is I can't find a way to add an OR condition in between multiple AND conditions.
Can anyone help me to code above where condition using Magento addAttributeToFilter()? or any other functions?
If i'm understanding you correctly I think you need to do some variation of this:
->addAttributeToFilter(...filter here...)
->addAttributeToFilter(array(
array(
'attribute' => 'special_to_date',
'date' => true,
'from' => $dateTomorrow
),
array(
'attribute' => 'special_to_date',
'null' => 1
)
));
which would be:
...filter here... AND (special_to_date >= '2012-07-03' OR special_to_date IS NULL)...

Resources