From 1ee42f28d1f0fc1c640909af68be27cba058f926 Mon Sep 17 00:00:00 2001
From: coresh <1753595@gmail.com>
Date: Mon, 7 Mar 2016 11:05:56 +0000
Subject: [PATCH] Improved Form.php. Updated method 'validateData'

Currently updated version of Diglin_Username_Model_Form class method
`
validateData(array $data)
`
allowed (not ignored) ajax requests validation
---
 .../community/Diglin/Username/Model/Form.php  | 35 +++++++++++++++++--
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/src/app/code/community/Diglin/Username/Model/Form.php b/src/app/code/community/Diglin/Username/Model/Form.php
index 2a7ae8e..6023826 100644
--- a/src/app/code/community/Diglin/Username/Model/Form.php
+++ b/src/app/code/community/Diglin/Username/Model/Form.php
@@ -50,10 +50,39 @@ public function extractData(Zend_Controller_Request_Http $request, $scope = null
      */
     public function validateData(array $data)
     {
+        $request = Mage::app()->getRequest();
+        $errors = array();
+
+        if ($request->isXMLHttpRequest()) {
+            $customerData = array();
+
+            /** @var $customer Mage_Customer_Model_Customer */
+            $customer = Mage::registry('current_customer');
+            if (is_null($customer)) {
+                $customer = Mage::getModel('customer/customer');
+            }
+
+            /** Check if registration from checkout page */
+            if ($request->getParam('checkout_page_registration', false)) {
+                $formCode = 'checkout_register';
+            } else {
+                $formCode = 'customer_account_create';
+            }
+
+            $this->setFormCode($formCode)->setEntity($customer);
+
+            $customerData = $this->extractData($request);
+            $data = array_merge($data, $customerData);
+
+            if ($request->getParam('is_subscribed', false)) {
+                $customer->setIsSubscribed(1);
+            }
+        }
+
         $errors = parent::validateData($data);
 
         // Prevent to change/save the username if it is not allowed on the frontend to change the username
-        if (!Mage::getStoreConfigFlag('username/general/frontend') && !Mage::app()->getStore()->isAdmin()) {
+        if (!Mage::getStoreConfigFlag('username/general/frontend') && !Mage::app()->getStore()->isAdmin() && $errors !== true && !empty($errors)) {
             return $errors;
         }
 
@@ -75,7 +104,7 @@ public function validateData(array $data)
             }
 
             // Prevent possible errors
-            if (empty($customerId)) {
+            if (empty($customerId) && $errors !== true && !empty($errors)) {
                 return $errors;
             }
 
@@ -111,7 +140,7 @@ public function validateData(array $data)
                 $validate = '/^*$/';
                 switch ($inputValidation) {
                     case 'default':
-                        $validate = '/^[\w-]*$/';
+                        $validate = '/^[\w\-_]*$/';
                         break;
                     case 'custom':
                         $validate = Mage::getStoreConfig('username/general/input_validation_custom');