Magento2: Image uploader in category form
I have add image uploader in dynamic template (admin category form) and trying to save the image in pub/media path.
In phtml file:
<?php
$_htmlId = 'categorymodel';
$_htmlClass = 'input-text admin__control-text required-entry _required';
$_htmlName = 'model_information';
?>
<div class="control">
<tr id="attribute-options-table">
<td colspan="10" class="data-grid">
<table class="admin__control-table tiers_table" id="tiers_table">
<tbody id="<?= /* @escapeNotVerified */
$_htmlId ?>_container"></tbody>
<tfoot>
<tr>
<td colspan="4" class="a-right" id="modelAddButton">
<button id="modelAddButton" title="Add New Model" type="button"
class="action-default scalable add" data-ui-id="additional-info-add-button">
<span>Add New Model</span>
</button>
</td>
</tr>
</tfoot>
</table>
</td>
</tr>
<script>
require([
'jquery',
'mage/template',
"jquery/ui",
'jquery/validate',
'mage/mage',
"prototype"
], function (jQuery, mageTemplate) {
var modelRowTemplate = '<tr>'
+ '<td><input type="hidden" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][model_id]" value="" id="models_{{index}}_model_id" data-form-part="category_form" />'
+ '<input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-entry" type="text" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][model_name]" value="" id="models_{{index}}_model_name" /></td>'
+ '<td><input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-sku" type="text" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][sku_list]" value="" id="models_{{index}}_sku_list" /></td>'
// + '<td><input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-entry" type="file" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][image]" value="" id="models_{{index}}_image" /></td>'
+ '<td class="last"><input data-form-part="category_form" type="hidden" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][delete]" class="delete" value="" id="models_{{index}}_delete" />'
+ '<button title="<?= /* @escapeNotVerified */ $block->escapeHtml(__('Delete Model')) ?>" type="button" class="scalable delete icon-btn delete-product-option" id="models_{{index}}_delete_button" onclick="return modelControl.deleteItem(event);">'
+ '<span><span><span><?= /* @escapeNotVerified */ __("Delete") ?></span></span></span></button></td>'
+ '</tr>';
var modelControl = {
template: new Template(modelRowTemplate, new RegExp('(^|.|\r|\n)({{\s*(\w+)\s*}})', "")),
itemsCount: 0,
addItem: function () {
var data = {
model_id: '',
model_name: '',
sku_list: '',
image: '',
index: this.itemsCount++
};
data.model_id = arguments[0];
data.model_name = arguments[1];
data.sku_list = arguments[2];
data.image = arguments[3];
Element.insert($('<?= /* @escapeNotVerified */ $_htmlId ?>_container'), {
bottom: this.template.evaluate(data)
});
if (data.model_id) {
$('models_' + data.index + '_model_id').value = data.model_id;
}
if (data.model_name) {
$('models_' + data.index + '_model_name').value = data.model_name;
}
if (data.sku_list) {
$('models_' + data.index + '_sku_list').value = data.sku_list;
}
// if (data.image) {
// $('models_' + data.index + '_image').value = data.image;
// }
},
deleteItem: function (event) {
var tr = Event.findElement(event, 'tr');
if (tr) {
Element.select(tr, '.delete').each(function (elem) {
elem.value = '1'
});
Element.select(tr, ['input', 'select']).each(function (elem) {
elem.hide()
});
Element.hide(tr);
Element.addClassName(tr, 'no-display template');
}
return false;
}
};
jQuery("#modelAddButton").on("click", function () {
modelControl.addItem();
});
<?php
if ($block->getCategoryModels()) :
foreach ($block->getCategoryModels() as $model)://echo "<pre>";print_r($model->getData());
?>
modelControl.addItem('<?php echo $model->getId() ?>', '<?php echo $model->getModelName() ?>', '<?php echo $model->getSkuList() ?>', '<?php echo $model->getImage() ?>');
<?php
endforeach;
endif;
?>
window.modelControl = modelControl;
});
</script>
</div>
So, I trying to get $_FILES in save controller.
but, It returns $_FILES array is empty {"exception":"[object] (Exception(code: 0): $_FILES array is empty .
If anything i want to change it..
Thanks.
magento2.2 php-7.1
add a comment |
I have add image uploader in dynamic template (admin category form) and trying to save the image in pub/media path.
In phtml file:
<?php
$_htmlId = 'categorymodel';
$_htmlClass = 'input-text admin__control-text required-entry _required';
$_htmlName = 'model_information';
?>
<div class="control">
<tr id="attribute-options-table">
<td colspan="10" class="data-grid">
<table class="admin__control-table tiers_table" id="tiers_table">
<tbody id="<?= /* @escapeNotVerified */
$_htmlId ?>_container"></tbody>
<tfoot>
<tr>
<td colspan="4" class="a-right" id="modelAddButton">
<button id="modelAddButton" title="Add New Model" type="button"
class="action-default scalable add" data-ui-id="additional-info-add-button">
<span>Add New Model</span>
</button>
</td>
</tr>
</tfoot>
</table>
</td>
</tr>
<script>
require([
'jquery',
'mage/template',
"jquery/ui",
'jquery/validate',
'mage/mage',
"prototype"
], function (jQuery, mageTemplate) {
var modelRowTemplate = '<tr>'
+ '<td><input type="hidden" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][model_id]" value="" id="models_{{index}}_model_id" data-form-part="category_form" />'
+ '<input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-entry" type="text" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][model_name]" value="" id="models_{{index}}_model_name" /></td>'
+ '<td><input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-sku" type="text" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][sku_list]" value="" id="models_{{index}}_sku_list" /></td>'
// + '<td><input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-entry" type="file" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][image]" value="" id="models_{{index}}_image" /></td>'
+ '<td class="last"><input data-form-part="category_form" type="hidden" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][delete]" class="delete" value="" id="models_{{index}}_delete" />'
+ '<button title="<?= /* @escapeNotVerified */ $block->escapeHtml(__('Delete Model')) ?>" type="button" class="scalable delete icon-btn delete-product-option" id="models_{{index}}_delete_button" onclick="return modelControl.deleteItem(event);">'
+ '<span><span><span><?= /* @escapeNotVerified */ __("Delete") ?></span></span></span></button></td>'
+ '</tr>';
var modelControl = {
template: new Template(modelRowTemplate, new RegExp('(^|.|\r|\n)({{\s*(\w+)\s*}})', "")),
itemsCount: 0,
addItem: function () {
var data = {
model_id: '',
model_name: '',
sku_list: '',
image: '',
index: this.itemsCount++
};
data.model_id = arguments[0];
data.model_name = arguments[1];
data.sku_list = arguments[2];
data.image = arguments[3];
Element.insert($('<?= /* @escapeNotVerified */ $_htmlId ?>_container'), {
bottom: this.template.evaluate(data)
});
if (data.model_id) {
$('models_' + data.index + '_model_id').value = data.model_id;
}
if (data.model_name) {
$('models_' + data.index + '_model_name').value = data.model_name;
}
if (data.sku_list) {
$('models_' + data.index + '_sku_list').value = data.sku_list;
}
// if (data.image) {
// $('models_' + data.index + '_image').value = data.image;
// }
},
deleteItem: function (event) {
var tr = Event.findElement(event, 'tr');
if (tr) {
Element.select(tr, '.delete').each(function (elem) {
elem.value = '1'
});
Element.select(tr, ['input', 'select']).each(function (elem) {
elem.hide()
});
Element.hide(tr);
Element.addClassName(tr, 'no-display template');
}
return false;
}
};
jQuery("#modelAddButton").on("click", function () {
modelControl.addItem();
});
<?php
if ($block->getCategoryModels()) :
foreach ($block->getCategoryModels() as $model)://echo "<pre>";print_r($model->getData());
?>
modelControl.addItem('<?php echo $model->getId() ?>', '<?php echo $model->getModelName() ?>', '<?php echo $model->getSkuList() ?>', '<?php echo $model->getImage() ?>');
<?php
endforeach;
endif;
?>
window.modelControl = modelControl;
});
</script>
</div>
So, I trying to get $_FILES in save controller.
but, It returns $_FILES array is empty {"exception":"[object] (Exception(code: 0): $_FILES array is empty .
If anything i want to change it..
Thanks.
magento2.2 php-7.1
add a comment |
I have add image uploader in dynamic template (admin category form) and trying to save the image in pub/media path.
In phtml file:
<?php
$_htmlId = 'categorymodel';
$_htmlClass = 'input-text admin__control-text required-entry _required';
$_htmlName = 'model_information';
?>
<div class="control">
<tr id="attribute-options-table">
<td colspan="10" class="data-grid">
<table class="admin__control-table tiers_table" id="tiers_table">
<tbody id="<?= /* @escapeNotVerified */
$_htmlId ?>_container"></tbody>
<tfoot>
<tr>
<td colspan="4" class="a-right" id="modelAddButton">
<button id="modelAddButton" title="Add New Model" type="button"
class="action-default scalable add" data-ui-id="additional-info-add-button">
<span>Add New Model</span>
</button>
</td>
</tr>
</tfoot>
</table>
</td>
</tr>
<script>
require([
'jquery',
'mage/template',
"jquery/ui",
'jquery/validate',
'mage/mage',
"prototype"
], function (jQuery, mageTemplate) {
var modelRowTemplate = '<tr>'
+ '<td><input type="hidden" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][model_id]" value="" id="models_{{index}}_model_id" data-form-part="category_form" />'
+ '<input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-entry" type="text" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][model_name]" value="" id="models_{{index}}_model_name" /></td>'
+ '<td><input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-sku" type="text" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][sku_list]" value="" id="models_{{index}}_sku_list" /></td>'
// + '<td><input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-entry" type="file" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][image]" value="" id="models_{{index}}_image" /></td>'
+ '<td class="last"><input data-form-part="category_form" type="hidden" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][delete]" class="delete" value="" id="models_{{index}}_delete" />'
+ '<button title="<?= /* @escapeNotVerified */ $block->escapeHtml(__('Delete Model')) ?>" type="button" class="scalable delete icon-btn delete-product-option" id="models_{{index}}_delete_button" onclick="return modelControl.deleteItem(event);">'
+ '<span><span><span><?= /* @escapeNotVerified */ __("Delete") ?></span></span></span></button></td>'
+ '</tr>';
var modelControl = {
template: new Template(modelRowTemplate, new RegExp('(^|.|\r|\n)({{\s*(\w+)\s*}})', "")),
itemsCount: 0,
addItem: function () {
var data = {
model_id: '',
model_name: '',
sku_list: '',
image: '',
index: this.itemsCount++
};
data.model_id = arguments[0];
data.model_name = arguments[1];
data.sku_list = arguments[2];
data.image = arguments[3];
Element.insert($('<?= /* @escapeNotVerified */ $_htmlId ?>_container'), {
bottom: this.template.evaluate(data)
});
if (data.model_id) {
$('models_' + data.index + '_model_id').value = data.model_id;
}
if (data.model_name) {
$('models_' + data.index + '_model_name').value = data.model_name;
}
if (data.sku_list) {
$('models_' + data.index + '_sku_list').value = data.sku_list;
}
// if (data.image) {
// $('models_' + data.index + '_image').value = data.image;
// }
},
deleteItem: function (event) {
var tr = Event.findElement(event, 'tr');
if (tr) {
Element.select(tr, '.delete').each(function (elem) {
elem.value = '1'
});
Element.select(tr, ['input', 'select']).each(function (elem) {
elem.hide()
});
Element.hide(tr);
Element.addClassName(tr, 'no-display template');
}
return false;
}
};
jQuery("#modelAddButton").on("click", function () {
modelControl.addItem();
});
<?php
if ($block->getCategoryModels()) :
foreach ($block->getCategoryModels() as $model)://echo "<pre>";print_r($model->getData());
?>
modelControl.addItem('<?php echo $model->getId() ?>', '<?php echo $model->getModelName() ?>', '<?php echo $model->getSkuList() ?>', '<?php echo $model->getImage() ?>');
<?php
endforeach;
endif;
?>
window.modelControl = modelControl;
});
</script>
</div>
So, I trying to get $_FILES in save controller.
but, It returns $_FILES array is empty {"exception":"[object] (Exception(code: 0): $_FILES array is empty .
If anything i want to change it..
Thanks.
magento2.2 php-7.1
I have add image uploader in dynamic template (admin category form) and trying to save the image in pub/media path.
In phtml file:
<?php
$_htmlId = 'categorymodel';
$_htmlClass = 'input-text admin__control-text required-entry _required';
$_htmlName = 'model_information';
?>
<div class="control">
<tr id="attribute-options-table">
<td colspan="10" class="data-grid">
<table class="admin__control-table tiers_table" id="tiers_table">
<tbody id="<?= /* @escapeNotVerified */
$_htmlId ?>_container"></tbody>
<tfoot>
<tr>
<td colspan="4" class="a-right" id="modelAddButton">
<button id="modelAddButton" title="Add New Model" type="button"
class="action-default scalable add" data-ui-id="additional-info-add-button">
<span>Add New Model</span>
</button>
</td>
</tr>
</tfoot>
</table>
</td>
</tr>
<script>
require([
'jquery',
'mage/template',
"jquery/ui",
'jquery/validate',
'mage/mage',
"prototype"
], function (jQuery, mageTemplate) {
var modelRowTemplate = '<tr>'
+ '<td><input type="hidden" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][model_id]" value="" id="models_{{index}}_model_id" data-form-part="category_form" />'
+ '<input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-entry" type="text" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][model_name]" value="" id="models_{{index}}_model_name" /></td>'
+ '<td><input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-sku" type="text" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][sku_list]" value="" id="models_{{index}}_sku_list" /></td>'
// + '<td><input data-form-part="category_form" class="<?= /* @escapeNotVerified */ $_htmlClass ?> required-entry" type="file" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][image]" value="" id="models_{{index}}_image" /></td>'
+ '<td class="last"><input data-form-part="category_form" type="hidden" name="<?= /* @escapeNotVerified */ $_htmlName ?>[{{index}}][delete]" class="delete" value="" id="models_{{index}}_delete" />'
+ '<button title="<?= /* @escapeNotVerified */ $block->escapeHtml(__('Delete Model')) ?>" type="button" class="scalable delete icon-btn delete-product-option" id="models_{{index}}_delete_button" onclick="return modelControl.deleteItem(event);">'
+ '<span><span><span><?= /* @escapeNotVerified */ __("Delete") ?></span></span></span></button></td>'
+ '</tr>';
var modelControl = {
template: new Template(modelRowTemplate, new RegExp('(^|.|\r|\n)({{\s*(\w+)\s*}})', "")),
itemsCount: 0,
addItem: function () {
var data = {
model_id: '',
model_name: '',
sku_list: '',
image: '',
index: this.itemsCount++
};
data.model_id = arguments[0];
data.model_name = arguments[1];
data.sku_list = arguments[2];
data.image = arguments[3];
Element.insert($('<?= /* @escapeNotVerified */ $_htmlId ?>_container'), {
bottom: this.template.evaluate(data)
});
if (data.model_id) {
$('models_' + data.index + '_model_id').value = data.model_id;
}
if (data.model_name) {
$('models_' + data.index + '_model_name').value = data.model_name;
}
if (data.sku_list) {
$('models_' + data.index + '_sku_list').value = data.sku_list;
}
// if (data.image) {
// $('models_' + data.index + '_image').value = data.image;
// }
},
deleteItem: function (event) {
var tr = Event.findElement(event, 'tr');
if (tr) {
Element.select(tr, '.delete').each(function (elem) {
elem.value = '1'
});
Element.select(tr, ['input', 'select']).each(function (elem) {
elem.hide()
});
Element.hide(tr);
Element.addClassName(tr, 'no-display template');
}
return false;
}
};
jQuery("#modelAddButton").on("click", function () {
modelControl.addItem();
});
<?php
if ($block->getCategoryModels()) :
foreach ($block->getCategoryModels() as $model)://echo "<pre>";print_r($model->getData());
?>
modelControl.addItem('<?php echo $model->getId() ?>', '<?php echo $model->getModelName() ?>', '<?php echo $model->getSkuList() ?>', '<?php echo $model->getImage() ?>');
<?php
endforeach;
endif;
?>
window.modelControl = modelControl;
});
</script>
</div>
So, I trying to get $_FILES in save controller.
but, It returns $_FILES array is empty {"exception":"[object] (Exception(code: 0): $_FILES array is empty .
If anything i want to change it..
Thanks.
magento2.2 php-7.1
magento2.2 php-7.1
asked 2 mins ago
Mano MMano M
974219
974219
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f266035%2fmagento2-image-uploader-in-category-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f266035%2fmagento2-image-uploader-in-category-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown