Validation

Certified Associate Developer

Hey Guys,

I have a database table that contains teams that have applied to the competition. I want to check the validation, if I insert team and competition to apply that already exists in the table displays as "Already applied.

a!formLayout(
    label: "Apply Team to Competition",
    contents: {
      a!sectionLayout(
        label: "",
        contents: {
          a!columnsLayout(
            columns: {
              a!columnLayout(
                contents: {
                  a!pickerFieldRecords(
                    label: "Choose a Team",
                    recordType: 'recordType!{9590f767-061f-48ac-850e-63e14f7db91d}SGDC Dance Team',
                    value: ri!map.teamId,
                    saveInto: ri!map.teamId,
                    showRecordLinks: false,
                    maxSelections: 1,
                    required: true
                  )
                }
              ),
              a!columnLayout(
                contents: {
                  a!pickerFieldRecords(
                    label: "Choose a Competition",
                    recordType: 'recordType!{fa469098-7d29-4880-bf7d-18b43769b332}SGDC Dance Competition',
                    value: ri!map.compId,
                    saveInto: ri!map.compId,
                    showRecordLinks: false,
                    maxSelections: 1,
                    required: true,
                    validations: ""
                  )
                }
              )
            }
          )
        }
      )
    },
    buttons: a!buttonLayout(
      primaryButtons: {
        a!buttonWidget(
          label: "Apply",
          saveInto: {},
          submit: true,
          style: "PRIMARY",
          validate: true
        )
      },
      secondaryButtons: {
        a!buttonWidget(
          label: "Cancel",
          value: true,
          saveInto: ri!cancel,
          submit: true,
          style: "NORMAL",
          validate: false
        )
      }
    )
  )

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    As best as I can guess you're saying if the user selects both a team and a competition from the picker fields, but that team/competition already exists in the DB, then it displays a validation error.

    In this case you'd pick one of the two picker fields to add validation to (you could do the same in both but it would be a bit redundant); the best might be your "Competition" field as users would usually fill in the later field second. 

    In your validation you would:

    1. check to make sure both fields have been given a value.  If not, return a blank value.
    2. query the database to see if a single entry exists with both the teamId and compId.  (Note: this assumes that the registration info is kept in a single table, which might usually but not always be the case).
    3. If there **is** a single entry with both IDs (or whatever exact set of conditions would indicate that team is registered for that competition, anyway), you would have your logic return the text of your validation error.  Otherwise, return null.

    This is usually written like:

    validations: {
      if(
        or(
          a!isNullOrEmpty(ri!map.compId),
          a!isNullOrEmpty(ri!map.teamId)
        ),
        {},
        
        if(
          /* for this example the following hypothetical rule returns true/false values, however you could simply use a query entity rule that returns a TotalCount and check that it's > 0, etc. */
          rule!queryRule_CheckingTeamRegistration(
            compId: ri!map.compId,
            teamId: ri!map.teamId
          ),
          "Selected Team is already registered for Selected Competition",
          {}
        )
      )
    }