Mgen spa Engineering 2 tutorial (Part 1): validators and core verification

Source: Internet
Author: User

Back to the tutorial directory

For me, the reason for writing a spa project is that it is enough for tedious Interface Verification writing. If you ask me, "What is the most annoying ?", I will not hesitate to answer "Verify !". Yes, from the previous Windows Forms, the verification logic was almost self-written, to the overall WPF framework, although many types were added to "simplify" the entire verification logic, but I think there is still room for optimization. Now let's see how the SPA project simplifies the verification logic.

Directory

  • 1. validator Overview
  • 2. Core Verification
  • 3. Overlay Verification
  • 4. String validators
  • 5. digit range validators

 

 

Returned directory
1. validator Overview

The validator is used to verify the data returned by the control. All validators in the spa project are in the mgen. spa. validators namespace.

Like controls, validators are also defined as. NET features. The validators have the following naming rules:

[For type] [name] Rule

 

For example, the frequently used validators whose judgment string cannot be empty are of the stringrequiredrule type.

 

The following are the types in the mgen. spa. validators namespace (except that spavalidatorattribute is an internal type, others are the validator feature execution ):

 

Let's take a simple example to generate an interface that requires a string, and then set a validator that requires the string not to be empty: stringrequiredrule. Code:

// + Using mgen. spa;

// + Using mgen. spa. controls;

// + Using mgen. spa. validators;

Class myspatype

{

[Spaheader ("key1")] // interface text key

[Textboxstringcontrol] // Control

[Stringrequiredrule] // validators

Public String text {Get; set ;}

}

 

Generate the interface result:

 

Note that the verification occurs immediately after the interface is displayed, because the Interface Generation option is used:

Onloadvalidation. Required

 

If you do not want this, you can use:

Onloadvalidation. None

The page generation option is described in Part 1 of the tutorial. If you forget it, you can refer to it again.

 

Returned directory
2. Core Verification

Before talking about other validators, readers may have questions about how to write their own verification logic? The SPA project supports a user-defined validators (which will be discussed later). However, the data verification of the SPA project does not rely solely on the validators, and another verification method is supported: core verification.

Core verification is to directly write the verification logic into the core definition, and the data core dependency of the SPA project. so the core verification is through capturing.. Net Property. Therefore, developers can directly throw an exception in the attribute set block. The SPA project will regard it as a verification error, such as defining such a type and attribute:

// + Using mgen. spa;

// + Using mgen. spa. controls;

// + Using mgen. spa. validators;

Class myspatype

{

String _ text;

 

[Spaheader ("key1")] // interface text key

[Textboxstringcontrol] // Control

Public String text

{

Get {return _ text ;}

Set

{

If (value! = NULL & value. Contains ("mgen "))

Throw new exception ("cannot contain mgen ");

_ Text = value ;}

}

 

}

 

Run the test:

 

Returned directory
3. Overlay Verification

Multiple validators can be superimposed, including core verification. Superposition can create multiple conditions. For example, we need such a string:

1. It cannot be blank.

2. The length cannot exceed 5.

3. It cannot start with a number.

 

The following code uses the validator provided by the SPA project, coupled with custom core verification, to quickly meet your needs (the Code uses another validator called stringlengthatmost, which will be introduced soon, here we use it to demonstrate overlay verification)

// + Using mgen. spa;

// + Using mgen. spa. controls;

// + Using mgen. spa. validators;

Class myspatype

{

String _ text;

 

[Spaheader ("key1")] // interface text key

[Textboxstringcontrol] // Control

[Stringrequiredrule] // verify 1

[Stringlengthatmostrule (5)] // verify 2

Public String text

{

Get {return _ text ;}

Set

{

If (! String. isnullorempty (value) & char. isdigit (value [0]) // verify 3

Throw new exception ("cannot begin with a number ");

_ Text = value ;}

}

 

}

 

After running, the error is not displayed only when all three verifications are passed:

 

 

Next I will introduce the pre-defined validators in the spa project.

 

Returned directory
4. String validators

The first one is that the most commonly used string cannot be an empty validators: stringrequired. It also has a very useful function. If you set the trim attribute of the stringrequired validators to true, when determining whether the string is unprecedented, it also takes the blank characters before and after the string into consideration.

The following code sets the trim attribute of the stringrequired validator to true:

[Stringrequiredrule (TRIM = true)]

Public String text {Get; set ;}

 

Even if the user inputs a character, the string is still considered null if all the characters are blank (space, tab, and press Enter.

 

Another string-related validators are character string length validators. There are three related types:

Stringlengthrule: specify the maximum value-specify the minimum value.

Stringlengthatmostrule: 0-specifies the maximum value.

Stringlengthatleastrule: specify the minimum value-the maximum length supported by the control.

 

Obviously, the last two types are used to facilitate the definition of the first type. For example, if you need to specify a string that cannot exceed 5 characters, you can directly use the stringlengthatmost validator:

[Stringlengthatmostrule (5)]

If the stringlength validator is used, the maximum and minimum values must be defined:

[Stringlengthrule (0, 5)]

 

Returned directory
5. digit range validators

The SPA project supports three types of numbers: int, double, unsigned long. Each type has three validators. These three validators are similar to the character string length validators mentioned above and are named in this way:

[For type] rangerule: specifies the maximum value-specifies the minimum value.

[For type] atmostrule: 0-specifies the maximum value.

[For type] atleastrule: specifies the minimum value-the maximum length supported by the control.

 

For example, ulongatmostrule is the validator for the specified maximum value of the unsigned long type.

 

Do you want to use ulong or double to write a program? However, you will soon find that the SPA project does not support their controls. Only the sliderint control is related to values but only supports int, the solution to this problem is the next part of the content I started to talk about-data stream.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.