Field Validation not firing as expected - Forum - Rollbase - Progress Community

Field Validation not firing as expected

 Forum

Field Validation not firing as expected

  • Hi guys

    So I have a field validation:

    /*if('{!isvCountries}'==='' || '{!isvCountries}' === null){
      if('{!worldwide}' === 'false'){
    	return 'At least one Country needs to be selected!';	    
      }
    } else{
      return null;
    }*/
    
    
    /*if ('{!worldwide}' === 'false' && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){
      return 'At least one Country needs to be selected!';
    }*/
    
    /*if (!{!worldwide} && ('{!isvCountries}'.length >= 1 || '{!isvCountries}' === null)){
      return 'At least one Country needs to be selected!';
    }*/
    
    if (!{!worldwide} && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){
      return 'At least one Country needs to be selected!';
    }


    No I have tried every variation I can think of, debugging works fine. The result is exactly as expected. However, when creating a new record validation fires regardless of whether there is a value in countries or not. Seems strange to me.

  • A comprehensive javascript empty string check is as simple as 

    if (!'{!isvCountries}') {
    ...
    }

    Your code probably fails for the case when the value is 'undefined' which is different from null.

    Your final code could be...

    if (!{!worldwide} && !'{!isvCountries}'.trim()){
      return 'At least one Country needs to be selected!';
    }

  • Thanks for that mate, still getting the validation firing after selecting a country

  • Could you use the logging helpers and log the values in the validation code, to get a feel of the actual values when the problem occurs?

  • Using the field validations field debugger I get the following:

    When worldwide is selected:

    Countries:

    Worldwide: true

    When worldwide is not selected and a country exists:

    Debug

    Countries: 23404

    Worldwide: false

    Result

    NULL

  • I tried your scenario and it seems to work fine. Could you revisit everything once?

    What I tried: checkbox worldwide field, country picklist

    - validation trigger code is

    if (!{!worldwide} && !'{!country}'){

     return 'At least one Country needs to be selected!';

    }

    When worldwide selected, even if the country is not set, the record gets saved. Otherwise throws the validation error message.

    P.S: I just realized my suggested code has .trim() which is wrong(fails) when no country is selected.

  • Hi Santosh

    I have literally copied and pasted your code and changed the country to isvCountries:

    /*if('{!isvCountries}'==='' || '{!isvCountries}' === null){

     if('{!worldwide}' === 'false'){

    return 'At least one Country needs to be selected!';    

     }

    } else{

     return null;

    }*/

    /*if ('{!worldwide}' === 'false' && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){

     return 'At least one Country needs to be selected!';

    }*/

    /*if (!{!worldwide} && ('{!isvCountries}'.length >= 1 || '{!isvCountries}' === null)){

     return 'At least one Country needs to be selected!';

    }*/

    /*if (!{!worldwide} && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){

     return 'At least one Country needs to be selected!';

    }*/

    /*if (!{!worldwide} && !'{!isvCountries}'.trim()){

     return 'At least one Country needs to be selected!';

    }*/

    if (!{!worldwide} && !'{!isvCountries}'){

    return 'At least one Country needs to be selected!';

    }

    I works when debugging perfectly, but client side its just not firing correctly

  • Bump, anything else?

  • '{!isvCountries}' === null

    can never be true, since:

    '' === null = false

    'null' === null = false

    so if you place the not (!), the message will be shown because it turns to true.

    if(!('{isvCountries}'))

    can only be true when isvCountries is '' (empty string)

    try '{!isvCountries}' != ''

  • Nah mate, not working. validation doesnt fire at all. Even in debugging now its not firing...

    if (!{!worldwide} && '{!isvCountries}' != ''){

    return 'At least one Country needs to be selected!';

    }

  • Okay.. For our understanding. What is your usecase? When should this error be fired? If "country is checked and isvCountries has not a value selected" the statement should be:

    if('{!worldwide#value}' == 'true' && '{!isvCountries}' == '') { // error message}

    {!workdwide#value} gives true or false

    {!worldwide} gives the html representation of the checkbox. <-- think that is the cause

  • Ok, let me explain rather. Yes the use case is as you say, when worldwide is NOT checked and there is NO countries selected then throw validation error. Easy enough. The previous code worked perfectly through the debugger in every scenario but when going client side the validation would fire as expected for the above use case, however it would also fire when there was one or more countries selected and worldwide was unchecked. see below

    if ('{!worldwide}' === 'false' && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){

     return 'At least one Country needs to be selected!';

    }

    Your suggestion behaves in the same manner:

    if ('{!worldwide#value}' == 'false' && '{!isvCountries}' == ''){

    return 'At least one Country needs to be selected!';

    }

  • I tried the same with relationship fields (as you mentioned countrie(s) ). Have attached a demo app for the purpose as tried out on Rollbase public cloud. And it seems to work as you desire.

    Can you try out this app and see if it works as expected? The Company object is what you would create and select countries for the ISV Countries field. Selecting no country and unselecting worldwide throws the desired error. With countries selected the record saves fine.

    Please confirm with this app.Demo App_v2.xml 

  • Thanks Santosh

    Can you have a look at the screenshot and explain why the debugger is reporting no value for {!countries} token? This explains why the trigger is firing the way it is.

  • And whats even stranger is that I imported your app and the validation fired as expected, using the exact same code on my object it doesnt work (obviously because the countries token is not being passed correctly).