AnsweredAssumed Answered

Sugar 6.5 CE Dynamic Validation

Question asked by David López on Jun 19, 2016

Hello,

 

I am trying to perform a validation for the Accounts module which consists in do not allow to save the account if the fields of the billing/shipping addresses are not filled.

This could seem easy, however I have customized the fields of the addresses in order to change their type dynamically.

 

My country field is now a dropdown of countries. Thus, if I choose 'ESPAÑA' in the dropdown, the 'state', 'city' and 'postalcode' fields of the address change and the become to dropdowns too. Actually, they only change via JavaScript, I mean, I remove the <input type="text" id="..." name = "..."> elements from the DOM and put type <select id=".." name ="..."><option...></option></select>. If "ESPAÑA" is chosen as country, also an additional field "Comunidad autónoma" appears below this field. If any other country is selected, the types will become <input type="text"> again, and the field "Comunidad autónoma" will disappear.

 

Actually everything works well, and I am able to save all the fields in the database and display them well, but the validation. For instance, I first try to save the address with no country selected, so the fields are type <input type="text"> and the result is the following:

 

direccion_requerida_1.png

However, if now I choose "ESPAÑA", and try to save again, this is the result:

direccion_requerida_2.png

 

The messages of the validation are duplicated. They are still there due to the previous validation, and I just can't remove them from there.

 

Does anyone have got some idea to clean the messages from there?

 

The fields are called like that:

  • billing_address_country;
  • billing_address_c_autonoma_c;
  • billing_address_state;
  • billing_address_city;
  • billing_address_postalcode;

 

And this is the code which validates currently the fields (of course inside the check_form function which triggers when the form is saved):

 

var address_types = [ 'billing', 'shipping' ];

var addressesCheck = checkAddresses( 'EditView', address_types );

function checkAddresses( formName, address_types ) {

   var good = true;
   $.each( address_types, function(index, type) {

 

   //País
   good &= checkAddressField( formName, type, 'country', 'País requerido' );

   //Comunidad autónoma
   good &= checkAddressField( formName, type, 'c_autonoma_c', 'Comunidad autónoma requerida', function( t, f ) {

   var country = $( '#' + t + '_address_country').val();
   var select = $( '#' + t + '_address_' + f);
   var val = select.val();
   if( country === 'ESPAÑA' && !val ) {

       select.parent().find( 'input[name="' + t +'_address_' + f +'"]').each( function(i, e) {

        $(this).remove();
   });
   clear_all_errors();
   add_error_style( formName, (t + '_address_' + f), 'Comunidad autónoma requerida' );
   select.parent().append( '<input type="hidden" name="' + t +'_address_' + f +'" value="' + val + '">' );
   return false;
   }

   return true;
   }, true);

   //Província
   good &= checkAddressField( formName, type, 'state', 'Província requerida' );

   //Ciudad
   good &= checkAddressField( formName, type, 'city', 'Ciudad requerida' );

   //Cód. postal
   good &= checkAddressField( formName, type, 'postalcode', 'Cód. postal requerido' );

   });
   return good;
};

 

function checkAddressField( formName, type, field, message, checkFunction, customStyle ) {

   var _checkFunction = function( t, field ) {

   var value = $( '#' + t + '_address_' + field).val();
   if( !value ) {

   return false;
   }

   return true;
   };

   if( checkFunction ) {

   _checkFunction = checkFunction;
   }

 

   var result = _checkFunction( type, field );

   if( !result && !customStyle ) {

   var fieldName = type + '_address_' + field;
   add_error_style( formName, fieldName, message );
   }

   return result;
};

 

 

Outcomes