I would like to check that the json structure matches the following:
$I->seeResponseMatchesJsonType([
'user' => [
'id' => 'string',
'name' => 'string',
'avatar' => 'string'
]);
However it should also accept if the user is null. It works with primitive types:
$I->seeResponseMatchesJsonType([
'user' => 'null|string'
]);
But how can I do this with the nested array?
Related
I have a form on my .blade that has a filed "type_id" and it must a number between 0 and 4
is it possible to create a multiple size? for example 'size:0 || size:1 || size:2 || size:3 || size 4' ???
that's my function store: (but the most important part is the type_id field)
public function store(Request $request)
{
$toCreate = $request->validate([
'type_id' => ['required','integer','size:4'],
'name' => ['required', 'string', 'max:255'],
'partitaIva' => ['max:255'],
'codiceFiscale' => ['max:255'],
'sedeAmministrativa' => ['max:255'],
'indirizzoNotifica' => ['max:255'],
'referente' => ['max:255'],
'responsabile' => ['max:255'],
'telefono' => ['max:255'],
'fax' => ['max:255'],
'email' => ['max:255'],
'pec' => ['max:255'],
'capitale' => ['max:255'],
'nDipendenti' => ['max:255'],
'convenzioniDeleghe' => [],
'note' => []
]);
Administration::create($toCreate);
return redirect()->route('administrations.index')->with('success','Amministratore aggiunto con successo.');
}
You can use "between" validator. eg.
'type_id' => ['required', 'integer', 'between:1,4']
or
'type_id' => 'required|integer|between:1,4',
size is used to validate the length of array, string or a file.
For numbers "between", two approches:
Like #ericmp says, you have the "between" rule in form validation :
('type_id' => ['required', 'integer', 'size:4', 'between:0,4'])
But if you needs more rules, or custom rules in one, you can create a custom validation rule file:
php artisan make:rule MyRule
There is an example on this Laracast topic: Best way to validate range in Laravel?
There is laravel's documentation about Validation rules: Validation: Laravel Doc
I need to create mongodb collection while executing code, by checking whether the collection is exists or not.
I tried below things
Yii::$app->db->createCommand()-> createCollection("collection_name");
but collection not created.
Please help.
Issue resolved..
Yii::$app->db->createCommand()->createCollection("collection_name")->execute();
and index will added for collection as,
$collectionName = Yii::$app->db->getCollection("collection_name");
$collectionName->createIndexes([
'key' => ['id' => 'int'],
'name' => 'id_index'
],
[
'key' => ['id' => 'int', 'category' => 'text'],
'name' => 'id_category_index',
]);
I'm using laminas-inputfilter, laminas-filter and laminas-validator in my REST API.
My filter config looks like this, for example:
'input_filter_specs' => [
MyResource::class . '\\Validator' => [
'param1' => ...,
'param2' => ...,
'param3' => [
'required' => false,
'allow_empty' => false,
'filters' => [
'no_tags' => [
'name' => StripTags::class,
],
'no_newlines' => [
'name' => StripNewlines::class,
],
'trim' => [
'name' => StringTrim::class,
],
],
'validators' => [
'not_empty' => [
'name' => NotEmpty::class,
],
],
],
],
],
The filtering and validation also work perfectly so far. But what surprises me is: I make a request to the API that contains param1 and param2, I deliberately omit param3. However, if I now look at the array that my REST service receives, I find there:
Array
(
[param1] => one
[param2] => two
[param3] =>
)
This means that the optional parameter is automatically added and filled with an empty string. The behavior is a bit unfavorable for me, however, as it collides with my further processes.
Is there a possibility (e.g. via a config switch) to only validate input parameters if they are set and otherwise exclude them from the array?
You can try with validation groups feature: https://docs.laminas.dev/laminas-form/quick-start/#validation-groups.
You can set a validation groups array with only required parameters, then check if param3 is not empty and in that case add to validation group array.
Hi so I'm trying to save data through Cakephp3 framework. I have two tables Measures and MeasuresUsers. Measures has many MeasuresUsers
$data = [
'name' => $this->request->data['name'],
'description' => $this->request->data['description'],
'deleted' => $this->request->data['deleted'],
'chart_type' => $this->request->data['chart_type'],
'public' => $this->request->data['public'],
'user_id' => $this->request->data['user_id']
];
$measure = $this->Measures->newEntity($data,['associated' => ['MeasuresUsers']]);
$measure['measures_users'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
$this->Measures->save($measure)
I'm able to save data into Measures table but not into the associative table (MeasuresUsers). I'm not sure what I'm missing here, any help or comments is appreciated!
#HarryM. is close, but you want to put your data into the $data array before creating the entity (the associated tag is kinda pointless if you're not providing that data!), and since it's a hasMany relation, I think you need to have the data in an array of arrays:
$data = [
'name' => $this->request->data['name'],
'description' => $this->request->data['description'],
'deleted' => $this->request->data['deleted'],
'chart_type' => $this->request->data['chart_type'],
'public' => $this->request->data['public'],
'user_id' => $this->request->data['user_id'],
'measures_users' => [
[
'user_id' => $user['id'], // Should this be $this->request->data['user_id'] instead?
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public'],
],
],
];
$measure = $this->Measures->newEntity($data, ['associated' => ['MeasuresUsers']]);
$this->Measures->save($measure);
Coming from CakePHP 2.x, I see your associate model is MeasuresUsers.
When you're setting the associated data in
$measure['measures_users'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
Trying changing $measure['measures_users'] to $measure['MeasuresUsers']:
$measure['MeasuresUsers'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
CakePHP's model conventions usually assume camel casing of what you have in the database e.g. DB Table it movie_tickets and Cake would expect is as MovieTicket unless otherwise declared in the Model itself.
I have written an index on Elasticsearch with following fields.
$indexData = [
'index' => 'profiles',
'type' => 'profile',
'body' =>[
'id' => $data->id,
'first_name' => (string)$data->first_name,
'last_name' => (string)$data->last_name,
'phone_mobile' => $data->phone_mobile,
'company_name' => $data->company_name,
]
];
$indexed = $profileElastic->addIndex($indexData);
I have managed to filter them using "more_like_this".
But i need to filter them using the Field Name.
Example: Searching value is : 456 and field is:phone_mobile.
Scenario:
First i Need to check that 'phone_mobile' column is blank or not, if it is not blank, then check whether 'phone_mobile' column has the value '456',if it is true, return the corresponding Data Set.
Anyway to do that?.
Thanks in Advance