Widget instance is not full complete to create.
This error message is caused by a Magento 1.x bug.
Explanation
When this happens, after selecting Type and Theme/Package, there are two requests done:
http://www.domain.dev/index.php/admin/widget_instance/validate/?isAjax=true
and with the following posted Form Data:
form_key:4izvVF4VrYgxs6W3
type:yourmodule/yourwidgettype
package_theme:rwd/default
http://www.domain.dev/index.php/admin/widget_instance/edit/type/{{type}}/package/{{package}}/theme/{{theme}}/
Checking what happens:
1st request calls \Mage_Widget_Adminhtml_Widget_InstanceController::validateAction()
which will do $widgetInstance = $this->_initWidgetInstance()
then $widgetInstance->validate()
;
_initWidgetInstance()
will do:
$instanceId = $this->getRequest()->getParam('instance_id', null);
$type = $this->getRequest()->getParam('type', null);
$package = $this->getRequest()->getParam('package', null);
$theme = $this->getRequest()->getParam('theme', null);
Problem here is that there are no two parameters package and theme, but one named package_theme, which should have been split to find out the two needed. This is the bug.
But why we think sometimes this does not happen?
Let's check how the requests are launched:
The Continue button has onClick="setSettings('http://www.domain.dev/index.php/admin/widget_instance/edit/type/{{type}}/package/{{package}}/theme/{{theme}}/', 'type', 'package_theme')"
The function called is:
function setSettings(urlTemplate, typeElement, packageThemeElement) {
if (!editForm.validate()) {
return false;
}
typeElement = $F(typeElement).replace(/\//g, '-');
packageThemeElement = $F(packageThemeElement).split(/\//g);
setLocation(new Template(urlTemplate, /(^|.|\r|\n)({{(\w+)}})/).evaluate({
'type': typeElement,
'package': packageThemeElement[0],
'theme': packageThemeElement[1]
}));
}
Looking at the second part, we can see the split is done and url looks correct:
/index.php/admin/widget_instance/edit/type/yourmodule-yourwidget/package/rwd/theme/default/
But first, there is the editForm.validate()
part which somehow ajax calls the validateAction()
from \Mage_Widget_Adminhtml_Widget_InstanceController::validateAction
. Let's see back in the JS:
validate()
is defined in js/mage/form.js
and if there is a validationUrl
set, it will call _validate
.
_validate : function(){
new Ajax.Request(this.validationUrl,{
method: 'post',
parameters: $(this.formId).serialize(),
onComplete: this._processValidationResult.bind(this),
onFailure: this._processFailure.bind(this)
});
Here we can see that the validateAction
request is called and the package_theme
parameter is not split. onComplete
(ajax request complete) will do _processValidationResult
which will display the error message:
$('messages').innerHTML = response.message;
But why not everyone complains about this or why the error is not always visible ?
It all depends on the server speed. As you can see, there is no waiting before setLocation is called in the setSettings function.
So if the server is fast enough, the redirect will be done before validation is finnished and result parsed.
Convince yourself
Take any Magento 1.x instance you have (latest now is 1.9.3.1, I've tested on 1.7.0.2 and 1.9.3.1), go to CMS/Widgets > Add new widget instance
, use Chrome inspect element, go to Network tab, check Preserve logs, select XHR requests, click Continue and check the validation ajax response.
The error will be there and now we know!
