B4J Question [ABMaterial] Show SuccessMessage and ErrorMessage under conditions

luisg2401

Member
Licensed User
hello, I would like to know how can I make my own conditions to show error or success messages on a ABMInput, and if I can have multiple error messages or success messages that would activate only under certain conditions, for example, an ABMInput where I have a condition that it can't be empty, and can´t have more than 20 characters, for wich I would have the messages "this field can't be empty" and "this field can't have more than 20 characters" respectively.
 

Harris

Expert
Licensed User
Longtime User
When saving the edit form...

B4X:
        valueString = ABMGenUSERUserLogin.Text  ' check what the text is - valid?
        If ABMGenNotEmpty(valueString) = False Then
            page.Msgbox("ERR","Required field...","Enter a Valid User Login","OK",False,ABM.MSGBOX_POS_BOTTOM_CENTER,"")
            Return
        End If

B4X:
Sub ABMGenNotEmpty(value As String) As Boolean
    ' TODO by the programmer!
    If value.Length < 2 Then
        Return False
    Else
        Return True
    End If
End Sub

Create anything you want to validate entries... It is all up to you (ie. between 1 - 10, has text (not empty), text less than 20 chars, doesn't contain curse words..., etc)
 
Upvote 0

luisg2401

Member
Licensed User
Thanks for you answer, but it's not exactly what I'm looking for, I didn't explain myself correctly in my question. Let's take this example of ABMInput from the demo page of ABMaterial:

B4X:
Dim inp5 As ABMInput
inp5.Initialize(page, "inp5", ABM.INPUT_EMAIL, "Email", False, "")
inp5.WrongMessage = "Wrong"
inp5.SuccessMessage = "Correct"
page.Cell(5,1).AddComponent(inp5)

In this ABMInput example that has the type ABM.INPUT_EMAIL, it will show an input.WrongMessage if the text on the input is missing a '@' or a '.xx' at the end, what I wanted to know is how can I make my own number of conditions (not empty, less than 20 chars, missing a '-', etc) show the input.WrongMessage or input.SuccessMessage accordingly. For example:

B4X:
Dim inp5 As ABMInput
inp5.Initialize(page, "inp5", ABM.INPUT_TEXT, "Name", False, "")
inp5.WrongMessage = "Wrong"
inp5.SuccessMessage = "Correct"
page.Cell(5,1).AddComponent(inp5)

whit that ABMInput that has the type ABM.INPUT_TEXT, I would like to make my number of conditions in order to show the input.WrongMessage or input.SuccessMessage according to my number of conditions.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Have your tried to change the WrongMessage in e.g. the LostFocus event?

Something like this (untested code):
B4X:
Sub muInput_LostFocus()
   Dim myInput As ABMInput = page.Component("myInput")
   Dim txt As String = myInput.Text
   If txt.Length > 20 Then
       myInput.WrongMessage = "The text is to long!"
       myInput.Refresh
       Return
   End If
   If txt.Contains("@") = False Then
       myInput.WrongMessage = "this is not an email"
       myInput.Refresh
       Return
   End If
End Sub
 
Last edited:
Upvote 0

luisg2401

Member
Licensed User
Have your tried to change the WrongMessage in e.g. the LostFocus event?

Something like this (untested code):
B4X:
Sub muInput_LostFocus()
   Dim myInput As ABMInput = page.Component("myInput")
   Dim txt As String = myInput.Text
   If txt.Length > 20 Then
       myInput.WrongMessage = "The text is to long!"
       myInput.Refresh
       Return
   End If
   If txt.Contains("@") = False Then
       myInput.WrongMessage = "this is not an email"
       myInput.Refresh
       Return
   End If
End Sub


I tried out this code but it doesn't work for me, when the input loses focus and the data on the input doesn't meet the conditions, instead of turning red and showing the WrongMessage, it turns gray and doesn't show the WrongMessage.
 
Upvote 0

mindful

Active Member
Licensed User
I tried out this code but it doesn't work for me
Did you just copy pasted the code ?
You have to replace the sub name with your control id ... so Sub muInput_LostFocus will be inp5_LostFocus and in the first line page.Component("myInput") will be page.Component("inp5")
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I just did the test myself and it looks you have to tell the component if it is valid or not in that case:

B4X:
Sub Pwd1_LostFocus()
   Dim model As ABMModalSheet = page.modalsheet("signup")
   Dim inp As ABMInput = model.Content.Component("pwd1")
   If inp.Text.Length < 8 Then
       inp.WrongMessage = "To short"
       inp.Valid = ABM.VALID_FALSE ' <------------------------------
       inp.Refresh
       Return
   End If
   If inp.Text.Length > 20 Then
       inp.WrongMessage = "To long"
       inp.Valid = ABM.VALID_FALSE ' <------------------------------
       inp.Refresh
       Return
   End If
   inp.SuccessMessage = "Length ok!"
   inp.Valid = ABM.VALID_TRUE ' <------------------------------
   inp.Refresh  
End Sub

I just noticed that the SuccessMessage was positioned a bit to high. Should be fixed in 4.26+.
 
Last edited:
Upvote 0

luisg2401

Member
Licensed User
I just did the test myself and it looks you have to tell the component if it is valid or not in that case:

B4X:
Sub Pwd1_LostFocus()
   Dim model As ABMModalSheet = page.modalsheet("signup")
   Dim inp As ABMInput = model.Content.Component("pwd1")
   If inp.Text.Length < 8 Then
       inp.WrongMessage = "To short"
       inp.Valid = ABM.VALID_FALSE ' <------------------------------
       inp.Refresh
       Return
   End If
   If inp.Text.Length > 20 Then
       inp.WrongMessage = "To long"
       inp.Valid = ABM.VALID_FALSE ' <------------------------------
       inp.Refresh
       Return
   End If
   inp.SuccessMessage = "Length ok!"
   inp.Valid = ABM.VALID_TRUE ' <------------------------------
   inp.Refresh 
End Sub

I just noticed that the SuccessMessage was positioned a bit to high. Should be fixed in 4.26+.

This worked like a charm, thanks a lot! but there's a thing bothering me, since you mentioned that the SuccessMessage was a bit too high and should be fixed in 4.26+, I have to say that in my case the WrongMessage is a bit too low, here's an example:

upload_2018-4-26_12-6-39.png
 
Upvote 0
Top