WordPress REST API validation always considers arrays to be empty - wordpress

'methods' => ['POST', 'GET'],
'args' =>
[
'users' =>
[
'type' => 'array',
'minItems' => 1,
'items' =>
[
'type' => 'object',
'properties' =>
[
'user_login' =>
[
'type' => 'string',
'required' => true,
'validate_callback' => function($user_login)
{
error_log("login validation");
}
],
'user_email' =>
[
'type' => 'string',
'required' => false,
'validate_callback' => function($user_email)
{
error_log("email validation");
}
]
]
]
]
],
'callback' => function($request)
{
return $request->get_body_params();
}
]
As is, every request fails, saying that the users array is empty. ~~If I comment out the```minItems` line, the request succeeds and I can get the parameters in the callback; however, neither error log fires, so I can't validate the incoming data. I suspect that the main issue is whatever is causing the minItems to fail, as the API for some reason thinks there's no data, and thus nothing to validate, until the main callback fires.~~
Edit: minItems now works. The problem is otherwise the same: the args are treated as empty until the main callback fires, at which point they're populated correctly.

Validation callbacks can only be added to top-level args, not items of arrays or properties of objects. I'm going to make a feature request; I'll try to remember to update this post if it gets approved.

Related

Symfony Form - Collection Type with Checkboxes inside

I am using Symfony 5, I want to have a "User Edit" page in administration, in which I will change User Roles, I want to have checkboxes to define which role assign to user, so for that, I need Collection Type with CheckboxType entry inside (if I am true), but for first I can't use user roles array as value for collection type
$builder
->add('roles', CollectionType::class, [
'entry_type' => CheckboxType::class,
'entry_options' => [
'required' => false,
],
])
This throws error
Unable to transform value for property path "[0]": Expected a Boolean.
after that, I tried to use a model transformer to change the value, below is code how I did that
$builder->get('roles')
->addModelTransformer(new CallbackTransformer(
function($rolesAsArray){
$rolesAsArray = array_flip($rolesAsArray);
foreach($rolesAsArray as &$role){
$role = true; // I also tried to set key instead of value - true
}
return $rolesAsArray;
},
function($rolesAsString){
dump($rolesAsString);die;
}
));
After this, I didn't get an error but I get the form with this look
So I haven't any option to change labels, and even I am submitting a form with these fields it throws an error
Expected argument of type "array", "null" given at property path "roles".
I found a way to do this with Select Box, but I can't found any way to do it with Checkbox.
If you have any ideas tell me, please.
You can use ChoiceType :
$builder->add('roles', ChoiceType::class, array(
'label' => 'form.label.role',
'choices' => User::ROLES,
'choice_translation_domain' => 'user',
'multiple' => true,
'expanded' => true,
'required' => true,
));
In User entity:
const ROLES = array(
'roles.admin' => 'ROLE_ADMIN',
'roles.secretary' => 'ROLE_SECRETARY',
'roles.user' => 'ROLE_USER'
);

Dynamic query string params in Wordpress Rest API

I cant find any information in the Wordpress rest api documentation about how to pass in a dynamic string as part of a url, I know how to do an ID.
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<type>)', [
'args' => [
'type' => [
'description' => __('Type of notification to generate', $this->pluginName),
'type' => 'string',
'enum' => ['product_question']
]
],
[
'methods' => \WP_REST_Server::CREATABLE,
'callback' => [$this, 'create_item'],
'permission_callback' => [$this, 'create_item_permissions_check'],
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE)
],
'schema' => [$this, 'get_public_item_schema']
]);
The above is the code im using to try and register the route. The route should look something like this /notifications/product_question The last part of the url, would be the dynamic part, and would be one of an array of values

Error for Constraint added to Field does not show for field

I dynamically generate a form and add constraints (i.e. Choice).
$builder->add('test', 'choice', [
'choices' => [1, 'one', 2 => 'two'],
'required' => true,
'expanded' => true,
'error_bubbling' => true,
'cascade_validation' => true,
'label' => 'this_is_a_test',
'multiple' => false,
'constraints' => [
new NotBlank([
'groups' => ['Default']
]),
new Choice([
'min' => 1,
'choices' => [1, 2],
'groups' => ['Default']
])
]
]);
When submitting the form with empty data, the error shows up for the form, not the element of the form where i added the constraint (checked in profiler as well).
There is not Option atPath for those constraints and i add them directly to the field, so i do not get why they show up for the form.
That's what the error_bubbling option does (which you set to true in your form type):
If true, any errors for this field will be passed to the parent field or form. For example, if set to true on a normal field, any errors for that field will be attached to the main form, not to the specific field.

Cake3: Controller TestCase is black-holed

When I use the Security component in Cake3 I always get the error message: "The request has been black-holed" in my controller tests. It works as expected because the request is really black-holed in that case but I need a possibility to test my code anyway.
I found the following post about the same issue but in Cake2. Unfortunately I was not able to transfer it to Cake3 and maybe it's not possible to use the same approach here.
This is how my test case looks:
$data = [
'first_name' => 'Test First Name',
'last_name' => 'Test Last Name',
'gender' => Gender::MALE,
'role_id' => Role::ADMIN,
'email' => 'test#test.com',
'password' => '',
'status' => Status::ACTIVE,
'birthday' => '2015-01-01',
];
$this->post(['prefix' => 'admin', 'controller' => 'users', 'action' => 'edit', 1], $data);
Same problem occurs also for Csrf component but the solution should be quite similar so I will figure this out afterwards.
Your test must to extend IntegrationTestCase and before doing the post you could use:
$this->enableCsrfToken();
$this->enableSecurityToken();

Sonata Admin - A2LiX Translation Field + Sonata Formatter Type

I've plugged Formatter (with CKeditor) to the 'content' field in one of my Sonata's Admin classes. This 'content' also has a translation, that's editable through 'a2lix_translations_gedmo' translations type. I've been trying to add CKeditor to this field as well, but it throws exceptions in any configuration I'm trying to set.
Google knows nothing about it, as well as SO. I've also looked in Sonata News Bundle sources (where sonata_formatter_type is implemented), but there are no translations available.
My Formatter field:
->add('content', 'sonata_formatter_type', [
'label' => "Content",
'event_dispatcher' => $formMapper->getFormBuilder()->getEventDispatcher(),
'format_field' => 'contentFormatter',
'source_field' => 'rawContent',
'ckeditor_context' => 'my_config',
'source_field_options' => [
'attr' => [
'class' => 'span10', 'rows' => 10
]
],
'listener' => TRUE,
'target_field' => 'content'
])
My Translation fields:
->add('translations', 'a2lix_translations_gedmo', [
'label' => "Управление локализациями",
'translatable_class' => 'AppBundle\Entity\Article',
'fields' => [
'content' => [
'locale_options' => [
'ru' => [
'label' => 'Контент'
]
]
]
]
])
Maybe someone knows how to add 'sonata_formatter_type' to this damn 'a2lix_translations_gedmo' type (or 'a2lix_translations')?
'a2lix_translations_gedmo' or 'a2lix_translations' depends of the translation strategy chosen.
Gedmo strategy is discouraged and you shoud use a more recent translation strategy like the KnpLabs that I recommend. https://github.com/KnpLabs/DoctrineBehaviors#translatable
Othewise, see https://github.com/a2lix/TranslationFormBundle/issues/177#issuecomment-94949480

Resources