added validation for delivery destination
This commit is contained in:
@@ -17,6 +17,7 @@ var _itemsInCart = new ItemInCartCollection();
|
||||
var _itemsForDisplay = new ItemCollection();
|
||||
_itemsForDisplay.setFromCart(true);
|
||||
var _deliveryDestination = new DeliveryDestination();
|
||||
var _deliveryDestinationErrors = {};
|
||||
|
||||
|
||||
var loadCart = function() {
|
||||
@@ -39,6 +40,7 @@ var loadCart = function() {
|
||||
if (!_deliveryDestination.get('id')) {
|
||||
_deliveryDestination.fetch({
|
||||
success: function() {
|
||||
validateDeliveryDestinationForm();
|
||||
CartActions.dataLoaded();
|
||||
}
|
||||
});
|
||||
@@ -82,6 +84,7 @@ var takeItemOut = function(itemId) {
|
||||
|
||||
var changeDeliveryDestinationProperty = function(property, value) {
|
||||
_deliveryDestination.set(property, value);
|
||||
validateDeliveryDestinationForm();
|
||||
};
|
||||
|
||||
|
||||
@@ -107,7 +110,49 @@ var saveDeliveryDestination = function() {
|
||||
})
|
||||
};
|
||||
|
||||
var validateDeliveryDestinationForm = function () {
|
||||
_deliveryDestinationErrors = {};
|
||||
|
||||
var nameRegex = /.+\s+.+/i;
|
||||
if(_deliveryDestination.get('name').search(nameRegex) < 0) {
|
||||
_deliveryDestinationErrors['name'] = "I prezime i ime su obavezni";
|
||||
}
|
||||
|
||||
var addressRegex = /.+\s+.+/i;
|
||||
if(_deliveryDestination.get('address').search(addressRegex) < 0) {
|
||||
_deliveryDestinationErrors['address'] = "Adresa mora biti ispravna";
|
||||
}
|
||||
|
||||
var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/i;
|
||||
if(_deliveryDestination.get('email').search(emailRegex) < 0) {
|
||||
_deliveryDestinationErrors['email'] = "Email mora biti ispravno upisan";
|
||||
}
|
||||
|
||||
var phoneRegex = /[\d\s-]{7,8}/i;
|
||||
if(_deliveryDestination.get('phone').search(phoneRegex) < 0) {
|
||||
_deliveryDestinationErrors['phone'] = "Telefon mora biti ispravan";
|
||||
}
|
||||
|
||||
var placeRegex = /^\s{0,1}\d{5}$/i;
|
||||
if(_deliveryDestination.get('place').search(placeRegex) < 0) {
|
||||
_deliveryDestinationErrors['place'] = "Mjesto mora biti izabrano";
|
||||
}
|
||||
|
||||
var requiredFields = ["name", "email", "place", 'address', 'phone'];
|
||||
for (var i in requiredFields) {
|
||||
var value = _deliveryDestination.get(requiredFields[i]);
|
||||
if (value === undefined || value === null || value === "") {
|
||||
// if it's required there will be a star there
|
||||
_deliveryDestinationErrors[requiredFields[i]] = "*";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
var isDeliveryDestinationValid = function () {
|
||||
return Object.getOwnPropertyNames(_deliveryDestinationErrors).length === 0;
|
||||
}
|
||||
// Extend CartStore with EventEmitter to add eventing capabilities
|
||||
var CartStore = _.extend({}, EventEmitter.prototype, {
|
||||
|
||||
@@ -137,7 +182,10 @@ var CartStore = _.extend({}, EventEmitter.prototype, {
|
||||
count: numberOfItems,
|
||||
items: _itemsForDisplay,
|
||||
itemCounts: states,
|
||||
deliveryDestination: _deliveryDestination
|
||||
deliveryDestination: _deliveryDestination,
|
||||
deliveryDestinationErrors: _deliveryDestinationErrors,
|
||||
isDeliveryDestinationValid: isDeliveryDestinationValid()
|
||||
|
||||
};
|
||||
return state;
|
||||
},
|
||||
@@ -156,7 +204,11 @@ var CartStore = _.extend({}, EventEmitter.prototype, {
|
||||
// Remove change listener
|
||||
removeChangeListener: function(callback) {
|
||||
this.removeListener('change', callback);
|
||||
}
|
||||
},
|
||||
|
||||
isDeliveryDestinationValid: isDeliveryDestinationValid
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user