HTML
public function visibleProductsAction()
{
$this->getResponse()->setBody(
$this->getLayout()->createBlock('module/adminhtml_block_type')->toHtml()
);
}
Generic Block:
$block = $this->getLayout()->createBlock('adminhtml/template')
->setTemplate('system/autocomplete.phtml')
->assign('items', $items);
$this->getResponse()->setBody($block->toHtml());
Array
class Mage_Adminhtml_JsonController extends Mage_Adminhtml_Controller_Action
{
/**
* Return JSON-encoded array of country regions
*
* @return string
*/
public function countryRegionAction()
{
$arrRes = array();
$countryId = $this->getRequest()->getParam('parent');
$arrRegions = Mage::getResourceModel('directory/region_collection')
->addCountryFilter($countryId)
->load()
->toOptionArray();
if (!empty($arrRegions)) {
foreach ($arrRegions as $region) {
$arrRes[] = $region;
}
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($arrRes));
}
/**
* Check is allowed access to action
*
* @return bool
*/
protected function _isAllowed()
{
return true;
}
}
Error Handling in Controller
Ref: app\code\core\Mage\Adminhtml\controllers\CustomerController.php
public function validateAction()
{
$response = new Varien_Object();
$response->setError(0);
// ...
if ($errors !== true) {
foreach ($errors as $error) {
$this->_getSession()->addError($error);
}
$response->setError(1);
}
// ...
$response->setError(1);
$this->_getSession()->addError(
Mage::helper('adminhtml')->__('Customer with the same email already exists.')
);
// ...
if ($response->getError()) {
$this->_initLayoutMessages('adminhtml/session');
$response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
}
$this->getResponse()->setBody($response->toJson());
}
./js/mage/adminhtml/tools.js
/**
* @todo add validation for fields
*/
function submitAndReloadArea(area, url) {
if($(area)) {
var fields = $(area).select('input', 'select', 'textarea');
var data = Form.serializeElements(fields, true);
url = url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true');
new Ajax.Request(url, {
parameters: $H(data),
loaderArea: area,
onSuccess: function(transport) {
try {
if (transport.responseText.isJSON()) {
var response = transport.responseText.evalJSON();
if (response.error) {
alert(response.message);
}
if(response.ajaxExpired && response.ajaxRedirect) {
setLocation(response.ajaxRedirect);
}
} else {
$(area).update(transport.responseText);
}
}
catch (e) {
$(area).update(transport.responseText);
}
}
});
}
}
./js/mage/adminhtml/form.js
varienForm.prototype = {
initialize : function(formId, validationUrl){
this.formId = formId;
this.validationUrl = validationUrl;
this.submitUrl = false;
if($(this.formId)){
this.validator = new Validation(this.formId, {onElementValidate : this.checkErrors.bind(this)});
}
this.errorSections = $H({});
},
checkErrors : function(result, elm){
if(!result)
elm.setHasError(true, this);
else
elm.setHasError(false, this);
},
validate : function(){
if(this.validator && this.validator.validate()){
if(this.validationUrl){
this._validate();
}
return true;
}
return false;
},
submit : function(url){
if (typeof varienGlobalEvents != undefined) {
varienGlobalEvents.fireEvent('formSubmit', this.formId);
}
this.errorSections = $H({});
this.canShowError = true;
this.submitUrl = url;
if(this.validator && this.validator.validate()){
if(this.validationUrl){
this._validate();
}
else{
this._submit();
}
return true;
}
return false;
},
_validate : function(){
new Ajax.Request(this.validationUrl,{
method: 'post',
parameters: $(this.formId).serialize(),
onComplete: this._processValidationResult.bind(this),
onFailure: this._processFailure.bind(this)
});
},
_processValidationResult : function(transport){
if (typeof varienGlobalEvents != undefined) {
varienGlobalEvents.fireEvent('formValidateAjaxComplete', transport);
}
var response = transport.responseText.evalJSON();
if(response.error){
if($('messages')){
$('messages').innerHTML = response.message;
}
}
else{
this._submit();
}
},
_processFailure : function(transport){
location.href = BASE_URL;
},
_submit : function(){
var $form = $(this.formId);
if(this.submitUrl){
$form.action = this.submitUrl;
}
$form.submit();
}
};
Posted by kiatng to OpenMage (2021-01-03 02:15)