ZF 2.4 File Validator Required False Doesn't Work - zend-framework

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

Related

Magento 2 admin custom save button send request twice

Basically, I want to send 'store' as a parameter when I save the form. For this, I customized the save button and added store_id there, but now save method is called twice. Any idea why this is happening and how can I fix it?
This is the button:(Vendor\Module\Block\Adminhtml\Entity\Edit\SaveButton.php)
public function getButtonData()
{
return [
'label' => __('Save'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => [
'buttonAdapter' => [
'actions' => [
[
'targetName' => 'vendor_module_entity_form.vendor_module_entity_form',
'actionName' => 'save',
'params' => [
true,
['store' => 5]
]
]
]
]
]
],
'sort_order' => 90,
];
}
and this is the ui_component (vendor_entity_entity_form.xml):
......
<settings>
<buttons>
<button class="Vendor\Module\Block\Adminhtml\Entity\Edit\SaveButton" name="save"/>
</buttons>
</settings>
<dataSource name="entiity_form_data_source">
<settings>
<submitUrl path="*/*/save"/>
<validateUrl path="*/*/validate"/>
</settings>
</dataSource>
......
Please try:
public function getButtonData(): array
{
return [
'label' => __('Save'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save']],
'form-role' => 'save',
],
'sort_order' => 10
];
}
please try the below code.
$this->buttonList->add(
'select_all',
[
'label' => __('Select All Rates and Save'),
'class' => 'save',
'onclick' => "jQuery('#testhidden').val(1)",
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save', 'target' => '#edit_form']],
]
],
10
);
Create a hidden field in the form.php where fields are defined
$fieldset->addField(
'testhidden',
'hidden',
['name' => 'testhidden', 'value' => 0, 'no_span' => true]
);

Problem with file upload TCA after upgrading to TYPO3 10

On typo3v9 i had working file upload field with this TCA configuration:
'image' => [
'exclude' => 0,
'label' => 'image upload',
'config' => [
'type' => 'group',
'internal_type' => 'db',
'uploadfolder' => 'uploads/folder',
'show_thumbs' => 1,
'size' => 5,
'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
'disallowed' => '',
],
],
i get this result:
but after migration to typo3v10 it does not work properly and give this result:
Someone have working solution for single file upload field?
I guess you mean internal_type=file ? That's deprecated. Here an example for image upload. However you can allow other types here too, I am sure you can find more info on that.
'photos' => [
'exclude' => true,
'label' => 'LLL:EXT:xxx/Resources/Private/Language/locallang_db.xlf:tx_xxx_domain_model_activity.photos',
'config' =>
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'photos',
[
'appearance' => [
'createNewRelationLinkTitle' => 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:images.addFileReference'
],
'foreign_types' => [
'0' => [
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
],
\TYPO3\CMS\Core\Resource\File::FILETYPE_IMAGE => [
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
]
],
'maxitems' => 30
],
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
),
],

Override TCA config for imageManipulation/crop for only one content type (CType)

I have three type of content elements (tt_content|types) which all use an image-column with each one FAL-relations for one image.
I'd like to use for 2 content elements the type = 'imageManipulation' (Docs) with 2 different configurations and for one just the image as it is.
Since the type = 'imageManipulation' is defined normally for sys_file_reference, so for all usages.
Is it possible with TCA overrides to archive different configurations for different content elements?
I tried a combination of columnsOverrides and overrideChildTca, but this doesn't work in the moment:
<?php
defined('TYPO3_MODE') or die();
(function () {
if (is_array($GLOBALS['TCA']['tt_content']['types']['mask_teaser_hero'])) {
$GLOBALS['TCA']['tt_content']['types']['mask_teaser_hero']['columnsOverrides'] = [
'tx_maskproject_teaserimage' => [
'config' => [
'overrideChildTca' => [
'columns' => [
'crop' => [
'label' => 'LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_file_reference.crop',
'config' => [
'type' => 'imageManipulation',
'cropVariants' => [
'mobile' => [
'title' => 'Mobile',
'selectedRatio' => '4:3',
'allowedAspectRatios' => [
'4:3' => [
'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.4_3',
'value' => 4 / 3
],
],
],
'desktop' => [
'title' => 'Desktop',
'selectedRatio' => '16:9',
'allowedAspectRatios' => [
'16:9' => [
'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.16_9',
'value' => 16 / 9
],
],
],
]
],
],
]
],
]
]
];
}
})();
I first thought about Typoscript TCEFORM:
https://metinyilmaz.de/artikel/typo3-image-cropvariants/
But this would also appear in each content element.
I found the mistake. The TCA override is correct. But the type was not.
I use EXT:mask_export for the content elements. In the example from the question I override the content elements which EXT:mask adds. But the exported content elements are different content elements.
The correct one is:
<?php
defined('TYPO3_MODE') or die();
(function () {
if (is_array($GLOBALS['TCA']['tt_content']['types']['myextname_teaser_hero'])) {
$GLOBALS['TCA']['tt_content']['types']['myextname_teaser_hero']['columnsOverrides'] = [
'tx_myextname_teaserimage' => [
'config' => [
'overrideChildTca' => [
'columns' => [
'crop' => [
'label' => 'LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_file_reference.crop',
'config' => [
'type' => 'imageManipulation',
'cropVariants' => [
'mobile' => [
'title' => 'Mobile',
'selectedRatio' => '4:3',
'allowedAspectRatios' => [
'4:3' => [
'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.4_3',
'value' => 4 / 3
],
],
],
'desktop' => [
'title' => 'Desktop',
'selectedRatio' => '16:9',
'allowedAspectRatios' => [
'16:9' => [
'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.16_9',
'value' => 16 / 9
],
],
],
]
],
],
]
],
]
]
];
}
})();

Zend routing issue

I'm working on converting a (quite sloppily put together) zend expressive website to a zend framework 3 website for a local restaurant. When I set up the routing on the expressive website I would load a location based on a query parameter looking like this.
website.com/location?location=xxx
On my new website the routing looks like this
website.com/locations/view/xxx
I need to set up a route that redirects the old url to the new url. So far I have set up a route that looks for the
/location?location=[/:location]
hoping that it would recognize this 'Segment' route and load the appropriate LocationsController. Right now it is giving me a route not found error.
My code looks like below.
module.config.php
namespace Application;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'locations-old' => [
'type' => Segment::class,
'options' => [
'route' => '/location?location=[/:location]',
'defaults' => [
'controller' => Controller\LocationController::class,
'action' => 'index',
],
],
],
'locations' => [
'type' => Segment::class,
'options' => [
'route' => '/locations[/:action[/:location]]',
'constraints' => [
'action' => '[a-zA-Z]*',
'location' => '[a-zA-Z]*',
],
'defaults' => [
'controller' => Controller\LocationController::class,
'action' => 'index',
],
],
],
'locations.html' => [
'type' => Literal::class,
'options' => [
'route' => '/locations.html',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'location' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'about' => [
'type' => Literal::class,
'options' => [
'route' => '/about',
'defaults' => [
'controller' => Controller\AboutController::class,
'action' => 'index',
],
],
],
'employ' => [
'type' => Literal::class,
'options' => [
'route' => '/employ',
'defaults' => [
'controller' => Controller\EmployController::class,
'action' => 'index',
],
],
],
'news' => [
'type' => Literal::class,
'options' => [
'route' => '/news',
'defaults' => [
'controller' => Controller\NewsController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => [
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];
LocationController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Application\Model\StoreTable;
use Zend\View\Model\ViewModel;
class LocationController extends AbstractActionController
{
private $table;
public function __construct(StoreTable $table)
{
$this->table = $table;
}
public function indexAction()
{
if($_GET['location'] && $this->table->doesExist($_GET['location'])) {
$location = $_GET['location'];
$this->redirect()->toRoute('locations', ['action' => 'view', 'location' => $location])->setStatusCode(302);
} else {
$this->redirect()->toUrl('/#locations')->setStatusCode(301);
}
}
public function viewAction()
{
$location = (string) $this->params()->fromRoute('location');
$store = $this->table->getStore($location);
return new ViewModel([
'store' => $store,
]);
}
}
Any help would be greatly appreciated and I can provide more info if needed.
Thanks!
configure your route as following, this will be handle query based url as you given "website.com/location?location=xxx",
In below route ":key" variable implies ?location=xxx
'locations-old' => [
'type' => Segment::class,
'options' => [
'route' => '/location/:key',
'defaults' => [
'controller' => Controller\LocationController::class,
'action' => 'index',
],
'constraints' => [
'key' => '[a-z0-9]+',
],
],
'child_routes' => [
'query' => ['type' => 'query'],
],
]

Cant use tockens and extrapattern together for REST services in Yii2

Yii2 REST query
I found this for using custom action in the controller for that i added the extrapattern mentioned in the above link
And its working fine when we search .but cant use the normal actions for the controller
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'extraPatterns' => [
'GET search' => 'search'
],
'tokens' => [
'{id}' => '<id:\\w+>'
]
]
],
]
Regards
Thanks all
this solved my problem after lots of trying..
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'extraPatterns' => [
'GET search' => 'search'
],
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'tokens' => [
'{id}' => '<id:\\w+>'
]
],
],