Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation using Lambda Expressions
    primarykey
    data
    text
    <p>I wanted to make a nice validation system in VB.NET and ASP.NET, where developers only need to specify a single line to validate their controls. I came up with a system, using lambda expressions. So basically, users would add their controls like this:</p> <p>ValidationHandler.CurrentInstance.AddValidationRule(Function() txtFirstName.Text.Length > 0, txtFirstName)</p> <p>This works perfectly with Windows Form Applications. I tried it on ASP.NET and the issue I have, is that on the second time I validate, it still holds the previous values. So for example, I have a textbox that has 0 length when it loads, it validates invalid. But, if I change it's length and click submit again, it still sees the textbox's length as 0.</p> <p><img src="https://i.stack.imgur.com/yMEmI.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/W0EBL.png" alt="enter image description here"></p> <p>And this is the problem. </p> <p>Also, this is the code for ValidationHandler:</p> <pre><code>Imports System.Text Public Class ValidationHandler 'This are the rules. Private ValidationRules As New Dictionary(Of Func(Of Boolean), Control) 'This is the error string builder. This will be empty if there are no errors. Public ErrorMessage As String 'This allows us to access the class without having to instantiate it. (Singleton pattren). Public Shared Property CurrentInstance As New ValidationHandler ''' &lt;summary&gt; ''' Adds a validation rule. ''' &lt;/summary&gt; ''' &lt;param name="Rule"&gt; ''' The rule. ''' &lt;/param&gt; ''' &lt;param name="control"&gt; ''' The control that validation is being applied on. ''' &lt;/param&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Public Sub AddValidationRule(ByRef Rule As Func(Of Boolean), Control As Control) 'Adds that rule to the dictionary. ValidationRules.Add(Rule, Control) End Sub ''' &lt;summary&gt; ''' This function does all validation on the controls. ''' &lt;/summary&gt; ''' &lt;returns&gt;True if there are no errors and false if there are.&lt;/returns&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Public Function IsValid() As Boolean 'Clears the error message string builder. ErrorMessage = "" 'Validates each rule against the control. For Each rule In ValidationRules If rule.Key.Invoke Then Dim str = "yay" Else 'Gets the error message from the tag, if the tag is not empty. ErrorMessage = ErrorMessage &amp; ("- " &amp; rule.Value.ID &amp; " is invalid." &amp; "&lt;/br&gt;" &amp; Environment.NewLine) End If Next 'Checks if there are any errors. If there are, return false, else return true. If ErrorMessage.Length &gt; 0 Then Return False Else Return True End If End Function 'Makes the class unable to be instantiated. Private Sub New() End Sub Public Sub Clean() ValidationRules.Clear() End Sub End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload