field condition

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 à 20% du montant demandé pour le prêt.",

                null

              ),

              null

             

 

            )

          }

        ),

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    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.

  • 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 à 20% du montant demandé pour le prêt.",
    
    null
    
    ),
    
    null
    
    
    
    
    
    )

  • 0
    Certified Lead Developer
    in reply to hrshkar
    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.
Reply
  • 0
    Certified Lead Developer
    in reply to hrshkar
    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.
Children
No Data