ApiGility - Getting validation errors with PUT - zend-framework

I am new to ApiGility and am attempting to update my shopping cart via an API call as apposed to a route. I am using Zend Framework 2 with a code connected api.
The problem I am currently facing is that no matter what I try, I am unable to put the information to the api without validation errors.
My module.config:
Updatecart settings:
'Api\\V1\\Rest\\Updatecart\\Controller' => array(
'listener' => 'Api\\V1\\Rest\\Updatecart\\UpdatecartResource',
'route_name' => 'api.rest.updatecart',
'route_identifier_name' => 'updatecart_id',
'collection_name' => 'updatecart',
'entity_http_methods' => array(
0 => 'GET',
1 => 'PATCH',
2 => 'PUT',
3 => 'DELETE',
),
'collection_http_methods' => array(
0 => 'GET',
1 => 'POST',
2 => 'PUT',
3 => 'PATCH',
4 => 'DELETE',
),
'collection_query_whitelist' => array(
0 => 'prod_id',
1 => 'quantity',
2 => 'quantity_accumulation',
3 => 'tax',
),
'page_size' => 25,
'page_size_param' => null,
'entity_class' => 'Api\\V1\\Rest\\Updatecart\\UpdatecartEntity',
'collection_class' => 'Api\\V1\\Rest\\Updatecart\\UpdatecartCollection',
'service_name' => 'updatecart',
),
Relevant filter settings:
'Api\\V1\\Rest\\Updatecart\\Validator' => array(
0 => array(
'name' => 'prod_id',
'required' => true,
'filters' => array(
0 => array(
'name' => 'Zend\\Filter\\Int',
'options' => array(),
),
),
'validators' => array(),
'description' => 'The id of the product in the cart to be updated',
'error_message' => 'Missing product id',
'allow_empty' => false,
'continue_if_empty' => false,
),
1 => array(
'name' => 'quantity',
'required' => true,
'filters' => array(),
'validators' => array(),
'description' => 'quantity of product',
'error_message' => 'You must include a quantity',
'allow_empty' => false,
'continue_if_empty' => false,
),
2 => array(
'name' => 'tax',
'required' => true,
'filters' => array(),
'validators' => array(),
'description' => 'Add the VAT, GST, Sales Tax that will be applicable to this item. Use 0.00 for no value.',
'error_message' => 'Please add a tax value, 0.00 for no value.',
'allow_empty' => false,
'continue_if_empty' => false,
),
3 => array(
'name' => 'quantity_accumulation',
'required' => true,
'filters' => array(
0 => array(
'name' => 'Zend\\Filter\\Boolean',
'options' => array(),
),
),
'validators' => array(),
'description' => 'Either accumulate the entered quantity to the current basket quantity or set as the entered quantity.',
'allow_empty' => false,
'continue_if_empty' => false,
'error_message' => 'Quantity accumulation field error.',
),
),
When calling the PUT method:
https://cloud.mysite.dev:8890/api/updatecart/1?prod_id=1&quantity=1&update_type=1&tax=0.00
I keep getting Failed Validation errors:
{
"validation_messages": {
"prod_id": [
"Missing product id"
],
"quantity": [
"You must include a quantity"
],
"tax": [
"Please add a tax value, 0.00 for no value."
],
"quantity_accumulation": [
"Quantity accumulation field error."
]
},
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "Unprocessable Entity",
"status": 422,
"detail": "Failed Validation"
}

You need to supply your data as http body (payload) in json format.
Make the call to the URI without the query params.
https://cloud.mysite.dev:8890/api/updatecart/1
{
"prod_id": 1,
"quantity": 1,
"update_type": 1,
"tax": "0.00"
}
Also make sure you supply the right request headers:
Accept: application/json
Content-Type: application/json

Related

How to Hide or Show Custom attribute based on other custom attribute in Product detail/edit page from Adminpanel Magento 2.4?

I have created the Product's custom attributes programmatically(Setup->Installdata.php).
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'serial_code_use_customer',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Issue By Customer Group',
'input' => 'boolean',
'class' => '',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '0',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable',
'group' => self::GROUP_LABEL,
'sort_order' => 3,
'note' => 'Enable automatic issuing of codes based on customer group.'
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'serial_code_customer_groups',
[
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Select Customer Groups',
'input' => 'multiselect',
'class' => '',
'source' => \Magento\Customer\Model\Customer\Attribute\Source\Group::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '0',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable',
'group' => self::GROUP_LABEL,
'sort_order' => 4,
'note' => 'Customers in selected groups will be issued codes automatically when product is ordered.'
]
);
I want to Hide or Show a Custom attribute based on other custom attributes in the Product detail/edit page from Adminpanel Magento 2.4.x?
For example, there is a toggle on click (yes/no) the Customer Groups will be Hide / Show.
To hide custom product attributes from the admin product detail page you need to create UpgradeData.php in the My/Catalog/Setup directory.
Code:
$eavSetup->updateAttribute(Product::ENTITY, $attrCode, 'is_visible',
'0');

Disabling the past dates

I want to disable the past dates from the laravel-backpack component date-range-picker.
`$this->crud->addField([
'name' => ['event_start_date', 'event_end_date'],
'type' => 'date_range',
'label' => "Start Date",
'wrapperAttributes'=>['class'=>'form-group col-md-6'],
'default' => [date("Y-m-d"), date("Y-m-d")],
'date_range_options' => [
'todayBtn' => 'linked',
'timePicker' => true,
'format' => 'YYYY-MM-DD'
],
'allows_null' => true,
]);`
How can I do that?
$this->crud->addField([
'name' => ['event_start_date', 'event_end_date'],
'type' => 'date_range',
'label' => "Start Date",
'wrapperAttributes'=>['class'=>'form-group col-md-6'],
//'default' => [date("Y-m-d"), date("Y-m-d")],
'date_range_options' => [
'todayBtn' => 'linked',
// options sent to daterangepicker.js
//'timePicker' => true,
'startDate' => date("Y-m-d"),
'endDate' => date("Y-m-d"),
'minDate'=> date("Y-m-d"),
'locale' => ['format' => 'YYYY-MM-DD']
],
'allows_null' => true,
]);

rewrite POST method on rest api yii2

I must rewrite post request on rest yii2.
Every time i've post request on url v1/availability
I want to call actionCreate for manage request.
This is my main.php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/availability'],
'pluralize' => true,
'extraPatterns' => [
'POST v1/availability' => 'v1/availability/create'
]
],
'OPTIONS v1/user/login' => 'v1/user/login',
'POST v1/user/login' => 'v1/user/login',
'POST v2/user/login' => 'v2/user/login',
'OPTIONS v2/user/login' => 'v2/user/login',
],
],
In the v1/controllers/AvailabilityController.php
i've
public function actions()
{
$actions = parent::actions();
unset($actions['view']);
return array_merge(
$actions,
[
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'prepareDataProvider' => [$this, 'index']
],
]
);
}
public function actionCreate(){
throw new \yii\web\HttpException(200, 'IT WORKS!', 200);
}
Any idea?
add url rule outside like below.
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/availability']
],
'POST v1/availability' => 'v1/availability/create',
'OPTIONS v1/user/login' => 'v1/user/login',
'POST v1/user/login' => 'v1/user/login',
'POST v2/user/login' => 'v2/user/login',
'OPTIONS v2/user/login' => 'v2/user/login',
],
],
And here you need understand rules of Yii. if you add one rule like below.
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
this open many urls for clients.
[
'PUT,PATCH users/<id>' => 'user/update',
'DELETE users/<id>' => 'user/delete',
'GET,HEAD users/<id>' => 'user/view',
'POST users' => 'user/create',
'GET,HEAD users' => 'user/index',
'users/<id>' => 'user/options',
'users' => 'user/options',
]
above all urls will open for clients.
So...
there are 2 way to override post.
First #Irfan Ali method
This method permit to declare single call in main.php and work with single function in controller.
Second method is
in main.php declare general call to availability
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/availability'],
'pluralize' => true,
],
],
],
for intercept post method you must override in the controller
public function createAction($id){
throw new \yii\web\HttpException(200, 'You receive post or put', 200);
}
and in this function you must intercept if call is post you do anything.
I love first method!

ZF 2.4 File Validator Required False Doesn't Work

Today I updated to ZF 2.4 to use float validator but unfortunately i realized that my file upload form field gives unexpected error messages.
Here is my form object
$this->add([
'name' => 'profileimage',
'type' => '\Zend\Form\Element\File',
'attributes' => [
'id' => 'profileimage',
'class' => 'styled',
],
]
);
And Here is my validator
$inputFilter->add([
'name' => 'profileimage',
'required' => false,
'allow_empty' => true,
'priority' => 300,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => '\Zend\Validator\File\IsImage',
],
[
'name' => '\Zend\Validator\File\UploadFile',
],
[
'name' => '\Zend\Validator\File\ImageSize',
'options' => [
'minWidth' => 300,
'minHeight' => 300,
]
],
[
'name' => '\Zend\Validator\File\Size',
'options' => [
'max' => '20MB',
]
],
]
]);
As you see the image upload field is not required and may be empty. But in my form I get these errors:
array (size=1)
'profileimage' =>
array (size=4)
'fileIsImageNotReadable' => string 'File is not readable or does not exist' (length=38)
'fileUploadFileErrorNoFile' => string 'File was not uploaded' (length=21)
'fileImageSizeNotReadable' => string 'File is not readable or does not exist' (length=38)
'fileSizeNotFound' => string 'File is not readable or does not exist' (length=38)
How can I handle this issue? I need to this field to be optional.
change your filter
$inputFilter->add([
'name' => 'profileimage',
'type' => '\Zend\InputFilter\FileInput',
'required' => false,
'allow_empty' => true,
'priority' => 300,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => '\Zend\Validator\File\IsImage',
],
[
'name' => '\Zend\Validator\File\UploadFile',
],
[
'name' => '\Zend\Validator\File\ImageSize',
'options' => [
'minWidth' => 300,
'minHeight' => 300,
]
],
[
'name' => '\Zend\Validator\File\Size',
'options' => [
'max' => '20MB',
]
],
]
]);
read about it here: http://framework.zend.com/manual/current/en/modules/zend.input-filter.file-input.html

zf2 form collection don't show element description

When the collection element is rendered , description element does not appear.
ZF2 seems to ignore the description option in the form collection.
Form
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'value',
'options' => array(
'label' => 'Please choose value',
'count' => 1,
'should_create_template' => true,
'target_element' => new ValueFieldset($objectManager)
)
));
Form Fieldset: ValueFieldset
$this->add(array(
'name' => 'name',
'type' => 'Zend\Form\Element\Text',
'options' => array(
'label' => 'Values',
'label_attributes' => array(
'class' => 'form-label'
),
'description' => 'X',
'multiple' => true,
),
)
);
View
echo $this->formCollection($values);