Skip to main content
yasminea4528
April 3, 2024
Question

field condition

  • April 3, 2024
  • 5 replies
  • 0 views

Hello everyone,

Here is the code for a field. When the user gives an input, there are some controls that should be verified. I want to add some code so that if the user gives an input that doesn't satisfy the condition, the field should be initialized as empty. How can I do this, please?

a!integerField(

          label: "Montant Apport",

          labelPosition: "ABOVE",

          placeholder: "Donner le montant exact de l'apport",

          value: ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{0f648b9f-079f-483f-87ad-d8a64c53802e}montantApport'],

          saveInto: ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{0f648b9f-079f-483f-87ad-d8a64c53802e}montantApport'],

          required: true(),

          validations: {

            if(

              and(

                not(isnull(ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{0f648b9f-079f-483f-87ad-d8a64c53802e}montantApport'])),

                not(isnull(ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{5c6f5fa8-85d3-4b23-af45-80a1b6b0c364}montantPret']))

              ),

              if(

                ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{0f648b9f-079f-483f-87ad-d8a64c53802e}montantApport'] < (0.2 * ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{5c6f5fa8-85d3-4b23-af45-80a1b6b0c364}montantPret']),

                "Le montant de l'apport doit être au moins égal Ã%A0 20% du montant demandé pour le prêt.",

                null

              ),

              null

             

 

            )

          }

        ),

    5 replies

    mikes0011
    Brainy
    April 3, 2024

    a!save() is your friend.

    You didn't really specify what condition you were imagining, so I'll go ahead and offer a straightforward example:

    a!localVariables(
      local!myInteger: null(),
      
      a!integerField(
        label: "Positive Integer Only",
        value: local!myInteger,
        saveInto: {
          a!save(
            local!myInteger,
            if(save!value < 0, null(), save!value)
          )
        }
      )
    )

    This will, of course, check the user input and, if it fails the condition (being greater than zero), it saves NULL into the local variable, and otherwise, saves the entered value.  The condition here is arbitrary - you can make the condition anything that you can write as the boolean condition of an if() statement.

    The Expression Guru
    yasminea4528
    April 3, 2024

    should i give the if condition in the saveInto instead of the validation ?  

    if(
    
    and(
    
    not(isnull(ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{0f648b9f-079f-483f-87ad-d8a64c53802e}montantApport'])),
    
    not(isnull(ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{5c6f5fa8-85d3-4b23-af45-80a1b6b0c364}montantPret']))
    
    ),
    
    if(
    
    ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{0f648b9f-079f-483f-87ad-d8a64c53802e}montantApport'] < (0.2 * ri!record['recordType!{3e104763-60d6-49a4-8cdb-405e073de678}PBY prêt.fields.{5c6f5fa8-85d3-4b23-af45-80a1b6b0c364}montantPret']),
    
    "Le montant de l'apport doit être au moins égal Ã%A0 20% du montant demandé pour le prêt.",
    
    null
    
    ),
    
    null
    
    
    
    
    
    )

    mikes0011
    Brainy
    April 4, 2024
    should i give the if condition in the saveInto instead of the validation ?

    They are mutually exclusive.  You can do either, but you need to pick one. 

    • If you just have the validation (which is the more common way), the field will just show the error message when an invalid value is entered. 
    • If you use the SaveInto override like I did above, then regardless of what you put for the validation, the field itself will not allow saving of an invalid value (making the validation message irrelevant, since it would not be possible to have a condition in which it shows).
      • Technically of course you can just have both, but as I mentioned, the validation won't effectively do anything in this case, so it only adds to code complexity and confusion by leaving it there when it's not doing anything.
    The Expression Guru
    April 3, 2024

    To initialize the field as empty if the user input doesn't satisfy the condition, you can add additional logic to the validation block.
    if condition inside the existing validation block. If the user input doesn't satisfy the condition (i.e., if the amount entered is less than 20% of montantPret), the a!save function is used to reset the value of the montantApport field to null, effectively initializing it as empty.

    mikes0011
    Brainy
    April 3, 2024
    I've added an if condition inside the existing validation block. If the user input doesn't satisfy the condition (i.e., if the amount entered is less than 20% of montantPret), the a!save function is used to reset the value of the montantApport field to null

    This would be an interesting approach, except it's nonsense.  You can't call a!save() inside the validations parameter.  At all.

    a!setValue(

    Additionally there is no such function as a!setValue().  Is your answer written by ChatGPT or something?

    The Expression Guru