locked
EntityValidationResultsBuilder RRS feed

  • Question

  • Hi,

    i want to make my own Validation

    how to use the ValidationResultsBuilder? i found nothing useful for me about it.

    is it possible to programmatically call the inbuilt Validation of Lightswitch?

    i have a property it will be stored as string in the db, but the user should be able to choose from datatype String/Number/Boolean, (so ist a kind of variant) and then it should validate accordingly. I have an entity Parameter my validate code looks like:

    Namespace LightSwitchApplication
    Public Class Parameter
    Private _boolDic As Collections.Generic.Dictionary(Of String, Boolean)
    Private Sub DefaultValue_Validate(results As EntityValidationResultsBuilder)
    	' results.AddPropertyError("<Errormessage>")
    	Select Case Me.Datatype
    		Case 5 'number 
    			Dim refDbl As Double
    			If Double.TryParse(Me.Standardwert, refDbl) Then
    				Me.DefaultValue = refDbl.ToString(Me.Formatstring)
    			Else
    				'How to use the messages of the Lightswitch-Validation here?
    			End If
    		Case 8 'char-sequence
    			'nothing to validate here
    		Case 11 'Boolean
    			If _boolDic Is Nothing Then _boolDic = New Dictionary(Of String, Boolean)
    			If _boolDic.Count = 0 Then
    				_boolDic.Add("WAHR", True)
    				_boolDic.Add("TRUE", True)
    				_boolDic.Add("OK", True)
    				_boolDic.Add("YES", True)
    				_boolDic.Add("FALSCH", False)
    				_boolDic.Add("FALSE", False)
    				_boolDic.Add("NO", False)
    				_boolDic.Add("WRONG", False)
    			End If
    			Dim strBol As String = Me.DefaultValue
    			Dim refBol As Boolean
    			If Not Boolean.TryParse(strBol, refBol) Then
    				strBol = strBol.ToUpper
    				If _boolDic.ContainsKey(strBol) Then
    					refBol = _boolDic.Item(strBol)
    				Else
    					'How to use the messages of the Lightswitch-Validation here?
    				End If
    			Else
    				'How to use the messages of the Lightswitch-Validation here?
    			End If
    			Me.DefaultValue = If(refBol, Boolean.TrueString, Boolean.FalseString)
    	End Select
    End Sub
    End Class

    how to implement the missing parts?

    regards

    Oliver



    • Edited by OlimilO Wednesday, May 1, 2013 11:05 AM
    Monday, April 29, 2013 2:36 PM

Answers

  • Hi Oliver,

    Most of the built-in validation messages are very generic and is not publicly exposed (as far as I know). You can simply use your own message and it actually can be better for the end user. For example

    If _boolDic.ContainsKey(strBol) Then
        refBol = _boolDic.Item(strBol)
    Else
        'How to use the messages of the Lightswitch-Validation here?
        results.AddPropertyError("Invalid Boolean value. Should be one of the following: WAHR, TRUE, OK, YES, FALSCH, FALSE, NO, WRONG")
    End If
    Best regards,
    Huy

    Thursday, May 2, 2013 5:19 PM

All replies

  • Hi Oliver,

    Most of the built-in validation messages are very generic and is not publicly exposed (as far as I know). You can simply use your own message and it actually can be better for the end user. For example

    If _boolDic.ContainsKey(strBol) Then
        refBol = _boolDic.Item(strBol)
    Else
        'How to use the messages of the Lightswitch-Validation here?
        results.AddPropertyError("Invalid Boolean value. Should be one of the following: WAHR, TRUE, OK, YES, FALSCH, FALSE, NO, WRONG")
    End If
    Best regards,
    Huy

    Thursday, May 2, 2013 5:19 PM
  • Thanks Huy for the answer
    Tuesday, May 7, 2013 6:48 AM
  • There is only one drawback.

    the events appear in this order: Changed, Validate

    If I have a EntityProperty of type double and give something wrong like "true" the inbuilt LS-Validation  says:

    "Der eingegebene Wert ("true") ist ungültig und wurde auf den
    vorherigen Wert zurückgesetzt. Konvertierungsfehler: Die
    Eingabezeichenfolge hat das falsche Format."

    it means:

    the value will be set to the value it was before, if the new value is not valid.

    But we never have the chance to get the value it was before, have we?

    in Changed and Validate-event the value is already the new (invalid) value

    regards

    Oliver

    Friday, May 10, 2013 9:55 AM
  • Ahhh, thanks Huy I found your answer here:

    MyEntity.Details.Properties.MyProperty.OriginalValue

    Thursday, May 16, 2013 11:28 AM