I have a trouble validating an array members in validation for Laravel. For array itself it passes, but for elements themselves, it fails.
Open json in console, shows structure of data being sent. It's clearly a integer. Since it's associative array, not sure how it even finds it's value as show in error handling red rectangle on the bottom of the page.
Here is my validation logic:
$validation = Validator::make(
$request->all(),
[
'load_place' => 'required|max:255',
"unload_place" => 'required|max:255',
"comment" => 'required|max:255',
'time_in' => 'required',
'time_out' => 'required',
"vehicles" => 'required|array',
"vehicles.*" => "required|integer",
'operator_id' => 'required|integer',
'sec_id' => 'required|integer'
]
);
And here is how it's returned:
if($validation->fails()){
$response = array(
"message" => "Failed",
"errors" => $errors,
"test" => $request->vehicles[0]
);
return response()->json($response);
}
That request[0], is that number at the bottom.
Edit1:
Keys in that marked array, are set on frontend, they are not not predefined. They can be whatever.
Change "vehicles.*" => "required|integer", to "vehicles.*.vehicle_id" => "required|integer", and it should work.
Keys is set on frontend, they are not not predefined. That what i am trying to say.
Try to replace "vehicles.*" => "required|integer", with new Rule::forEach rule for nested array data - guess it should work.
"vehicles.*" => Rule::forEach(function($value, $attribute) {
return [
Rule::integer()->min(1) //min(1) as eg if it's for an id field which cannot be 0
];
}),
Related
CakePHP Version: 4.0.1
Introduction
I have 2 methods that both use the index view, index and search. On index the column can be selected from a select list and a value can be inputted via an input form control enabling a search by column and value. This data is sent via GET to the search method where empty values are checked and the query is executed and the index view is rendered.
In the later 3x versions with the below configuration the index view had the sort on the selected column which is what it is meant to do.
IE: Index view has due_date sorted on the initial load and I select task_name then submit the form to the search method. The task_name has the sort when the view is rendered.
TASKS CONTROLLER
Public pagination property:
public $paginate = [
'sortWhitelist' => [
'Tasks.due_date',
'Tasks.task_name',
'Tasks.type',
'Tasks.priority',
'Tasks.related_to_name',
'Contacts.first_name',
'Contacts.last_name',
'Accounts.account_name',
'Tasks.task_desc'
]
];
Search Method
I initialise the data received from the index method and apply the config to the pagination property and send the query object to the view.
$this->setPage('');
$this->setSort($this->request->getQuery('column'));
$this->setDirection('asc');
// Validation of the page, sort, direction and limit is done here.
// IE: The $this->getSort() must be a string and not be numeric and has a strlen check
// and the $this->getDirection() can only be a string with values 'asc' or 'desc' etc.
if (!empty($this->getPage())) {
$this->paginate['page'] = $this->getPage();
}
$this->paginate['sort'] = $this->getSort();
$this->paginate['direction'] = $this->getDirection();
$this->paginate['limit'] = $this->getLimit();
debug($this->paginate);
$tasks = $this->paginate($query);
$this->set(compact('tasks'));
The result of debug is:
[
'sortWhitelist' => [
(int) 0 => 'Tasks.due_date',
(int) 1 => 'Tasks.task_name',
(int) 2 => 'Tasks.type',
(int) 3 => 'Tasks.priority',
(int) 4 => 'Tasks.related_to_name',
(int) 5 => 'Contacts.first_name',
(int) 6 => 'Contacts.last_name',
(int) 7 => 'Accounts.account_name',
(int) 8 => 'Tasks.task_desc'
],
'sort' => 'Tasks.task_name',
'direction' => 'asc',
'limit' => (int) 25
]
Result
The sort is on the task_name.
A couple of months ago I upgraded to 4 and have just revisted this functionality to find the sort is on the column that was present on index and not the column that was selected. I tried the below to fix the problem:
I referenced this information in the cookbook. And this from SO.
$config = $this->paginate = [
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit()
];
debug($config);
$tasks = $this->Paginator->paginate($query, $config);
debug($this->Paginator);
$this->set(compact('tasks'));
The result of debug $config is:
[
'page' => '',
'sort' => 'Tasks.task_name',
'direction' => 'asc',
'limit' => (int) 25
]
The result of debug $this->Paginator is:
object(Cake\Controller\Component\PaginatorComponent) {
'components' => [],
'implementedEvents' => [],
'_config' => [
'page' => (int) 1,
'limit' => (int) 20,
'maxLimit' => (int) 100,
'whitelist' => [
(int) 0 => 'limit',
(int) 1 => 'sort',
(int) 2 => 'page',
(int) 3 => 'direction'
]
]
}
NOTE: The whitelist contains limit, sort, page and direction? And the limit is 20 and I don't even have a selection of 20?
Result
The sort is on the due_date and I need it on the task_name.
Extra Info
If I then click the sort on task_name the sort is on the task_name. All the sorts work just not on the initial load?
Question
How can I configure the pagination property so the sort is on the task_name from the initial load of the search method.
Thanks Z.
The fix is a bit costly and not ideal but it does work. I do a redirect on the initial load. Basically submit the form to search then redirect back to search. IE:
if ($this->request->getQuery('initial') === 'yes') {
$redirect = $this->request->getQuery('redirect', [
'action' => 'search',
'?' => [
'method' => 'search',
'column' => $this->getColumn(),
'input' => $this->getInput(),
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit(),
'filter' => $this->getFilter(),
]
]);
return $this->redirect($redirect);
exit(0);
}
$config = $this->paginate = [
'sortWhitelist' => [
'Tasks.due_date',
'Tasks.task_name',
'Tasks.type',
'Tasks.priority',
'Tasks.related_to_name',
'Contacts.first_name',
'Contacts.last_name',
'Accounts.account_name',
'Tasks.task_desc'
],
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit()
];
$tasks = $this->Paginator->paginate($query, $config);
$this->set(compact('tasks'));
The sort is now on the task_name.
This negates the initial load problem and simulates usage after the page initially loads where I know the sorts work.
I have two request sample and incomplete sample, I get the complete sample by submitting the form without ticking any checkbox, then get incomplete samples by clicking the checkbox.
I can get incomplete samples by ticking the checkbox. I get the error while getting the complete samples.
if(!in_array($sample, $request->pending)){
$tests = Session('tests');
$createSample = Sample::create([
'test_user_id' => $request->test_user_id,
'received_by' => (integer)$request->received_by,
'received_at' => $now,
'received_name' => $request->received_name,
'biobank' => $request->biobank,
'order_id' => $request->order_id,
'order_type' => $request->order_type,
'name' => $request->name,
]);
Its because if you don't check the checkbox, the value won't submit and hence the $request->pending will be null instead of an array. You can try to check if its null and then you can do whatever you want to do with it.
if($request->pending){
if(!in_array($sample, $request->pending)){
$tests = Session('tests');
$createSample = Sample::create([
'test_user_id' => $request->test_user_id,
'received_by' => (integer)$request->received_by,
'received_at' => $now,
'received_name' => $request->received_name,
'biobank' => $request->biobank,
'order_id' => $request->order_id,
'order_type' => $request->order_type,
'name' => $request->name,
]);
}
}
from request I get an array like this:
'array' => [
0 => ['id' => 1,'val' => 2],
1 => ['id' => 1,'val' => 2]
]
I need to validate it so all ids of array will be unique.
right now I try this validation rule:
'array.*.id' => 'different:array.*.id'
but it will check current array with current array so result will be like
The array.0.id and array.0.id must be different.
You should use distinct rule:
'array.*.id' => 'distinct'
so I am trying to loop through an array of objects to update certain values in my database with the values from the object
When I run this loop in my controller
foreach($statuses as $status){
$workflow->statuses()->where('id', $status->id)->update([
'status' => $status->status
]);
};
It gives me error trying to get property of non-object
When I do a `return response($request->statuses) this is the data structure I see in the console
here is the complete controller
public function workflowStatuses(Request $request, Workflow $workflow)
{
// validate form data
$data = $request->validate([
'workflow' => 'required|string',
]);
// validate form data
$oldStatuses = $request->validate([
'statuses' => 'required|array'
]);
// validate form data
$newStatuses = $request->validate([
'newStatuses' => 'required|array',
]);
$workflow->update($data);
$statuses = $oldStatuses;
foreach($statuses as $status){
$workflow->statuses()->where('id', $status['id'])->update([
'status' => $status->status
]);
};
$workflow->statuses()->saveMany($newStatuses);
return response($workflow, 200);
}
You can think of the return value of $request->validate() as the array of all request input filtered to only include the data that's being validated. If that validated data contains arrays, you'll have a multi-dimensional array.
Here, $oldStatuses is going to be an array that contains a key named statuses that contains the actual array you're looking for.
$oldStatuses = $request->validate([
'statuses' => 'required|array'
]);
// $statuses should get 'statuses' out of this validated array
$statuses = $oldStatuses['statuses'];
Instead, you may want to clean this up and not call validate three times. It's usually better to run all the validation rules in one validate() call, unless you have good reason to logically separate them.
$validated = $request->validate([
'workflow' => 'required|string',
'statuses' => 'required|array',
'newStatuses' => 'required|array',
]);
$statuses = $validated['statuses'];
Try:
foreach($statuses as $status){
$workflow->statuses()->where('id', $status['id'])->update([
'status' => $status['status'] //$status['status'] should also be accessed by key
]);
};
I want to save a bunch of static records in my database with a given uuid, this is for testing purposes, so that on every system the application starts with the exact same dataset.
When inserting with SQL this is no problem but I wanted to use the CakePHP way ( I use a migrations file for this, but that does not matter).
The problem is that I give cake a data array like this and save it:
$data = [
['id' => '5cedf79a-e4b9-f235-3d4d-9fbeef41c7e8', 'name' => 'test'],
['id' => 'c2bf879c-072c-51a4-83d8-edbf2d97e07e', 'name' => 'test2']
];
$table = TableRegistry::get('My_Model');
$entities = $table->newEntities($data, [
'accessibleFields' => ['*' => true],
'validate' => false
]);
array_map([$table, 'save'], $entities );
Everything saves, but all my items have been given a different uuid, If I debug a record after saving it shows the original uuid in the entity
'new' => false,
'accessible' => [
'*' => true
],
'properties' => [
'id' => '6b4524a8-4698-4297-84e5-5160f42f663b',
'name' => 'test',
],
'dirty' => [],
'original' => [
'id' => '5cedf79a-e4b9-f235-3d4d-9fbeef41c7e8'
],
So why does cake generate a new uuid for me? and how do I prevent it
This doesn't work because primary keys are unconditionally being generated before the insert operation, see
https://github.com/cakephp/cakephp/blob/3.0.0/src/ORM/Table.php#L1486-L1490
// ...
$id = (array)$this->_newId($primary) + $keys;
$primary = array_combine($primary, $id);
$filteredKeys = array_filter($primary, 'strlen');
$data = $filteredKeys + $data;
// ...
$statement = $this->query()->insert(array_keys($data))
->values($data)
->execute();
// ...
Currently the UUID type is the only type that implements generating IDs, so providing custom IDs works with other types.
You can workaround this by for example overriding the _newId() method in your table so that it returns null, which effectively results in the existing primary key not being overwritten.
protected function _newId($primary)
{
// maybe add some conditional logic here
// in case you don't want to be required
// to always manually provide a primary
// key for your insert operations
return null;
}