Skip to content
This repository was archived by the owner on Oct 17, 2020. It is now read-only.

Improved Diglin_Username_Model_Form class method 'validateData' #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/app/code/community/Diglin/Username/Model/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -75,7 +104,7 @@ public function validateData(array $data)
}

// Prevent possible errors
if (empty($customerId)) {
if (empty($customerId) && $errors !== true && !empty($errors)) {
return $errors;
}

Expand Down Expand Up @@ -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');
Expand Down