Add validations for Hotel address when add a new hotel

This commit is contained in:
aman-webkul
2024-05-21 17:12:44 +05:30
parent fb5d0a32b8
commit 8a7170261c

View File

@@ -326,7 +326,39 @@ class AdminAddHotelController extends ModuleAdminController
if ($city == '') {
$this->errors[] = $this->l('City is required field.');
} elseif (!Validate::isCityName($city)) {
$this->errors[] = $this->l('Enter a Valid City Name.');
$this->errors[] = $this->l('Enter a valid city name.');
}
//Since the address for the hotel is saved in the address table. We are validating the hotel address here manually.
$addressValidation = Address::getValidationRules('Address');
foreach ($addressValidation['size'] as $field => $maxSize) {
if ('phone' == $field && Tools::strlen($phone) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel phone number is too long (%1$d chars max).'),
$maxSize
);
} else if ('address1' == $field && Tools::strlen($address) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel address is too long (%1$d chars max).'),
$maxSize
);
} else if ('city' == $field && Tools::strlen($city) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel city name is too long (%1$d chars max).'),
$maxSize
);
} else if ('postcode' == $field && Tools::strlen($zipcode) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel zip code is too long (%1$d chars max).'),
$maxSize
);
} else if (($value = Tools::getValue($field)) && Tools::strlen($value) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel %1$s field is too long (%2$d chars max).'),
$field,
$maxSize
);
}
}
if ($idHotel) {
@@ -378,13 +410,13 @@ class AdminAddHotelController extends ModuleAdminController
$linkRewriteArray = array();
foreach ($languages as $lang) {
if (!trim(Tools::getValue('hotel_name_'.$lang['id_lang']))) {
$objHotelBranch->hotel_name[$lang['id_lang']] = Tools::getValue(
$objHotelBranch->hotel_name[$lang['id_lang']] = trim(Tools::getValue(
'hotel_name_'.$defaultLangId
);
));
} else {
$objHotelBranch->hotel_name[$lang['id_lang']] = Tools::getValue(
$objHotelBranch->hotel_name[$lang['id_lang']] = trim(Tools::getValue(
'hotel_name_'.$lang['id_lang']
);
));
}
if (!trim(Tools::getValue('link_rewrite_'.$lang['id_lang']))) {
@@ -469,23 +501,36 @@ class AdminAddHotelController extends ModuleAdminController
}
// getHotel address
if ($idHotelAddress = $objHotelBranch->getHotelIdAddress()) {
$objAddress = new Address($idHotelAddress);
$objAddress = new Address($idHotelAddress);
} else {
$objAddress = new Address();
}
$objAddress->id_hotel = $newIdHotel;
$objAddress->id_country = $country;
$objAddress->id_state = $state;
$objAddress->city = $city;
$objAddress->postcode = $zipcode;
$hotelName = $objHotelBranch->hotel_name[$defaultLangId];
$objAddress->alias = $hotelName;
$hotelName = preg_replace('/[0-9!<>,;?=+()@#"°{}_$%:]*$/u', '', $hotelName);
$objAddress->lastname = $hotelName;
$objAddress->firstname = $hotelName;
$objAddress->alias = trim(substr($hotelName, 0, 32));
$hotelName = trim(preg_replace('/[0-9!<>,;?=+()@#"°{}_$%:]*$/u', '', $hotelName));
$addressFirstName = $hotelName;
$addressLastName = $hotelName;
// If hotel name is length is greater than 32 then we split it into two
if (Tools::strlen($hotelName) > 32) {
// Slicing and removing the extra spaces after slicing
$addressFirstName = trim(substr($hotelName, 0, 32));
// To remove the excess space from last name
if (!$addressLastName = trim(substr($hotelName, 32, 32))) {
// since the last name can also be an empty space we will then use first name as last name
$addressLastName = $addressFirstName;
}
}
$objAddress->firstname = $addressFirstName;
$objAddress->lastname = $addressLastName;
$objAddress->address1 = $address;
$objAddress->phone = $phone;
$objAddress->save();
// Save refund rules of the hotels