/** * Retrieve error message for the item * * @param Varien_Object $item * @return string */ public function getMessageByItem(Varien_Object $item) { $message = $this->getMessage($item->getCode()); return $message ? $message : $item->getError(); }
public function validateAction() { $response = new Varien_Object(); $response->setError(0); $websiteId = Mage::app()->getStore()->getWebsiteId(); $accountData = $this->getRequest()->getPost('account'); $customer = Mage::getModel('Mage_Customer_Model_Customer'); $customerId = $this->getRequest()->getParam('id'); if ($customerId) { $customer->load($customerId); $websiteId = $customer->getWebsiteId(); } else { if (isset($accountData['website_id'])) { $websiteId = $accountData['website_id']; } } /* @var $customerForm Mage_Customer_Model_Form */ $customerForm = Mage::getModel('Mage_Customer_Model_Form'); $customerForm->setEntity($customer)->setFormCode('adminhtml_customer')->setIsAjaxRequest(true)->ignoreInvisible(false); $data = $customerForm->extractData($this->getRequest(), 'account'); $errors = $customerForm->validateData($data); if ($errors !== true) { foreach ($errors as $error) { $this->_getSession()->addError($error); } $response->setError(1); } # additional validate email if (!$response->getError()) { # Trying to load customer with the same email and return error message # if customer with the same email address exisits $checkCustomer = Mage::getModel('Mage_Customer_Model_Customer')->setWebsiteId($websiteId); $checkCustomer->loadByEmail($accountData['email']); if ($checkCustomer->getId() && $checkCustomer->getId() != $customer->getId()) { $response->setError(1); $this->_getSession()->addError(Mage::helper('Mage_Adminhtml_Helper_Data')->__('Customer with the same email already exists.')); } } $addressesData = $this->getRequest()->getParam('address'); if (is_array($addressesData)) { /* @var $addressForm Mage_Customer_Model_Form */ $addressForm = Mage::getModel('Mage_Customer_Model_Form'); $addressForm->setFormCode('adminhtml_customer_address')->ignoreInvisible(false); foreach (array_keys($addressesData) as $index) { if ($index == '_template_') { continue; } $address = $customer->getAddressItemById($index); if (!$address) { $address = Mage::getModel('Mage_Customer_Model_Address'); } $requestScope = sprintf('address/%s', $index); $formData = $addressForm->setEntity($address)->extractData($this->getRequest(), $requestScope); $errors = $addressForm->validateData($formData); if ($errors !== true) { foreach ($errors as $error) { $this->_getSession()->addError($error); } $response->setError(1); } } } if ($response->getError()) { $this->_initLayoutMessages('Mage_Adminhtml_Model_Session'); $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml()); } $this->getResponse()->setBody($response->toJson()); }
public function validateAction() { $response = new Varien_Object(); $response->setError(false); $data = $this->getRequest()->getParams('subaccount'); $websiteId = Mage::app()->getStore()->getWebsiteId(); $subAccount = Mage::getModel('cminds_multiuseraccounts/subAccount'); if ($id = $this->getRequest()->getParam('id')) { $subAccount->load($id); $websiteId = $subAccount->getWebsiteId(); } else { if (isset($data['website_id'])) { $websiteId = $data['website_id']; } } $subAccount->addData($data); $errors = $subAccount->validate(); if ($errors !== true) { foreach ($errors as $error) { $this->_getSession()->addError($error); } $response->setError(true); } # additional validate email if (!$response->getError()) { # Trying to load customer with the same email and return error message # if customer with the same email address exisits $checkCustomer = Mage::getModel('customer/customer')->setWebsiteId($websiteId); $checkCustomer->loadByEmail($subAccount->getEmail('email')); $subAccountcheck = Mage::getModel('cminds_multiuseraccounts/subAccount')->setWebsiteId($websiteId); $subAccountcheck->loadByEmail($subAccount->getEmail('email')); if ($checkCustomer->getId() || $subAccountcheck->getId() && $subAccountcheck->getId() != $subAccount->getId()) { $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()); }