Laravel validation requiredIf with array index - php

I need to apply a required rule to a array field in my form request when the another array field, in the same index, has a certain value. Example:
public function rules(): array
{
return [
'data' => 'array',
'data.*.is_admin' => 'required|boolean',
'data.*.area' => [
'nullable',
Rule::requiredIf($condition),
// other rules...
]
]
}
When evaluating the first item in the array, if data.0.is_admin = 1, I'll add required to data.0.area. The problem is that I'm unable to access the current index inside the requiredIf method.
Using closures I was able to access the current index, but I can't override the nullable rule unless I add a required one and I need to be able to pass null to area. I can't use sometimes because I need to ignore the other rules when area = null.
Right now I'm looping through $request->data and adding the rules conditionally for each index. I wonder if there is a way I can achieve this using Laravel's array sintax.
Thank you

Related

required_if validation in Laravel

I am trying to add a rule to validate a field. The 'snippet' field needs to be required only if the body field contains HTML. I have the following in my controller store method, but I can't quite figure out how the required_if would work in conjunction with a regex for checking if the field contains HTML:
$newsitem->update(request()->validate([
'snippet' => 'nullable|max:255|not_regex:/[<>]/',
'body' => 'required',
]));
From the Laravel docs, it looks like the: 'required_if:anotherfield,value' validation rule returns a boolean based on a value, but I need to return a boolean based on a regex check of the body field.
I know I could check if the body is equal to one:
'snippet' => 'required_if:body,==,1|nullable|max:255|not_regex:/[<>]/'
...but I need something that checks if the body contains < or >.
(I could add a hidden field in my form submission which would get a value set if the body contained html using javascript, but I wanted to be able to handle this with backend validation.)
Do I need to use a custom validation rule, or can this be handled with the default Laravel validation rules alone?
I have seen in the Laravel docs that you can use the: Rule::requiredIf method to build more complex conditions. Is this something I can use alongside a contains_hmtl function?
'snippet' => Rule::requiredIf($request->snippet()->contains_html),
I really feel like I am missing something here, so any pointers would be much appreciated.
Try the following regex with RequiredIf:
[
...,
'snippet' => ['nullable', 'max:255', new RequiredIf(function () {
return preg_match('/<[^<]+>/m', request()->input('body')) !== 0;
})],
...,
]
RequiredIf rule accepts a closure which should return a boolean value.
Rule::requiredIf(function () use ($request) {
//Perform the check on $request->snippet to determine whether it is a valid HTML here
//Return true if it is valid HTML else return false
}),

How to pass current user ID to buildRules method of CakePHP model

I use AuthComponent with CakePHP 3.8 and now I need to do some logic in Model buildRules method but for this I need to get the current user ID.
Is there any way to pass/retrieve it without using hacks such as accessing directly from the session.
I know that it is possible to pass id via validator from controller as described in CakePHP's documentation
https://book.cakephp.org/3/en/core-libraries/validation.html#using-custom-validation-rules
And it works for validation, however, I am unable to access the validator from the inside of build rules.
When I do as described in here, I get an empty object.
https://book.cakephp.org/3/en/orm/validation.html#using-validation-as-application-rules
It seems that I am able to attach new validation rules but unable to retrieve the "Passed" provider to get the User ID.
It seems a trivial thing but a I spent quite a few hours trying to get the id in a proper way.
OK, After working a bit more, I found how to retrieve user_id inside build rules. Might be helpful to someone.
Do this in the controller
$this->ExampleModel->validator('default')->provider('passed', [
'current_user' => $this->Auth->user('id')
]);
And then put this in you buildRules method
public function buildRules(RulesChecker $rules)
{
$user_id = $this->validator('default')->provider('passed')['current_user'];
$rules->add(
function ($entity, $options) use($user_id) {
//return boolean for pass or fail
},
'ruleName',
[
'errorField' => 'some_id',
'message' => 'Some ID field is inconsistent with App logic.'
]
);
return $rules;
}
The proper way to handle this is usually using either events, or saving options.
For example to make the ID available for the application rules of all tables, you could do something like this:
$this->getEventManager()->on('Model.beforeRules', function (
\Cake\Event\Event $event,
\Cake\Datasource\EntityInterface $entity,
\ArrayObject $options
) {
$options['current_user'] = $this->Auth->user('id');
});
It would be available in the $options argument of the rule accordingly, ie as $options['current_user'].
For a specific save operation you can pass it to the options of the save() call:
$this->ExampleModel->save($entity, [
'current_user' => $this->Auth->user('id')
]);
There's also plugins that can help you with it, for example muffin/footprint.

Laravel: Validate field only when the value changed

Some form fields in my laravel 5.5 application have validation rules that run against a remote API and take quite some time. I would thus only want to run these expensive checks when the field value changes (is different from the value currently stored in the model).
Is there something that implements this already, e.g. as a rule similar to sometimes?
I would image it like this: only_changed|expensive_validation|expensive_validation2. The latter rules would only be executed when the field value has changed.
Assuming you are using a custom Request class, the rules() method expects an associative array to be returned before it applies the validation.
You can build your array of rules dynamically using the request contents before applying the validation like so:
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$validation_array = [
'name' => 'required|string|max:255',
];
if ($this->my_field === $some_condition) {
$validation_array = array_merge($validation_array. [
'my_field' => "required|expensive_validation|expensive_validation2"
]);
}
return $validation_array;
}
Note: I haven't run this yet but the principle should be fine.

php firebase api - delete children element from nested document

I'm using php sdk to delete some fields from firestore database,
i want to delete a map from an array inside a document, but instead the function used delete all maps inside the parent array.
My firestore database looks like this
What i'm trying to do is remove a specific index ex: index 0 with it's children from imageUrls array not all the maps inside it.
I tried these 2 functions :
$usersRef->update([
['path' => 'imageUrls.image_url', 'value' => FieldValue::arrayRemove(['image.png'])]
]);
and this one
$usersRef->update([
['path' => 'imageUrls.image_url', 'value' => FieldValue::deleteField()]
]);
The first function remove all imageUrls childrens and change imageUrls type from array to map, while the second one nothing happened. All the fields still exist in the document and no deletion occurd
<?php
namespace App\Services;
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\FieldValue;
use Google\Cloud\Firestore\FieldPath;
class FirebaseService
{
public function delete()
{
// Create the Cloud Firestore client
$db = new FirestoreClient(
['projectId' => 'MyProjectId']
);
$usersRef = $db->collection('NewStories')->document('1');
$usersRef->update(
[
['path' =>'imageUrls.image_url',
'value' => FieldValue::arrayRemove(['image.png'])]
]);
}
}
This can be achieved using the arrayRemove() method. As the PHP Client for Firestore says
Returns a special value that can be used with set(), create() or update()
that tells the server to remove the given elements from any array value
that already exists on the server. All instances of each element
specified will be removed from the array. If the field being modified is
not already an array it will be overwritten with an empty array.
Update:
Firebase does not support updating an existing element in an indexed array.
More information can be found in the Official Documentation.
Workaround:
Reading the entire array out of the document, make modifications to it in memory, then update the modified array field entirely.**
Credits to this Firestore Update single item in an array field case.

Laravel In Array validation

I am trying to validate a payment methods field.
Requirement is the field under validation must be of type array (Multiple values allowed) and the items should not be other than the defined option
'payment_method' => 'required|array|in:american_express,cash_on_delivery,paypal,paypal_credit_card,visa_master_card'
So the user should pass an array of values for e.g
array('american_express','paypal');
But should not pass
array('american_express', 'bank');
I am unable to find any such method in Laravel 4.1 documentation. Is there any work around for this ?
If you're using a later version of Laravel (not sure when the feature becomes available - but certainly in 5.2) you can simply do the following:
[
'payment_method' => 'array',
'payment_method.*' => 'in:american_express,cash_on_delivery,paypal,paypal_credit_card,visa_master_card'
];
You check that the payment_method itself is an array if you like, and the star wildcard allows you to match against any in the array input.
You might consider putting this into the rules() method of a request validator (that you can generate with php artisan make:request PaymentRequest as an example.
You can actually extend the Laravel validator and create your own validation rules. In your case you can very easily define your own rule called in_array like so:
Validator::extend('in_array', function($attribute, $value, $parameters)
{
return !array_diff($value, $parameters);
});
That will compute the difference between the user input array found in $value and the validator array found in $parameters. If all $value items are found in $parameters, the result would be an empty array, which negated ! will be true, meaning the validation passed. Then you can use the rule you already tried, but replace in with in_array like so:
['payment_method' => 'required|array|in_array:american_express,cash_on_delivery,paypal,paypal_credit_card,visa_master_card']

Categories