Saturday, July 1, 2017

Introducing the Scissors Validation Framework

You may download the Scissors Validation Framework from GitHub.

Validations is an essential part of interactive software development. We can get away without it, but unexpected things may happen when a user starts entering unexpected data. Having said that, validation is an oft neglected part of the development process. It's rarely an interesting part of writing software, and when developer's fatigue starts to creep in, becomes the least prioritized task. The system works with or without it, so lazy developers see it as an optional component, not an essential one.

To help combat this repetitive task of implementing validations to data entry modules, I've developed this validations framework to automate the configurations required to validate each field. This handles both client-side scripts validation as well as server-side validation. As there are varieties of jQuery-based validation libraries available, the framework has the flexibility to use one's preferred library, or even custom solutions.

The primary goals of this framework are:
  1. to automate some aspects of page validation across an entire project, and
  2. to ensure these validations are uniform, making it easier to make project-wide changes
The framework has similarities to .NET's built-in StringValidatorAttribute and other similar attributes, and in fact I thought I had found an out-of-the-box solution to what I wanted, but alas, this attribute is usable only for custom configuration sections. It could not be used for our own custom codes.

To start, consider a class named Employee, containing basic employee information.

For this class, we define validation rules using the StringValidatorAttribute (and other corresponding attributes) of the validations framework. To apply size validations to the name fields, we define them as follows:

The above definition indicates that the FirstName field should be between 5 to 50 characters in length, inclusive, and is a required field, while the MiddleName field should be between 5 to 20 characters in length, inclusive, but this time is an optional field as the IsRequired property is not set.

All the validator attributes have the same first constructor argument: the FieldLabel. The FieldLabel is the friendly display name when referring to this property during validation operations. For StringValidatorAttribute, the second and third arguments define the minimum and maximum text lengths. There are other optional settings for the various attributes, like the IsRequired, which defines the property as a required input, among others.

Below is the entire Employee class with appropriate validator attributes defined.

This configures the validation rules to be used for each field in the data entry pages of our application.

Of note is IntValidationAttribute, whose minimum/maximum values are overridden internally as needed, depending on the scope of the Int type decorated. For example, if this attribute is decorating an Int16 property but was defined with a maximum value more than the maximum value of an Int16, internally the validator shall override the defined maximum to instead use Int16.MaxValue.

On the web page where we receive input for Employee details, place textboxes for each of the Employee fields. Place a button to trigger the validation on postback. Additionally, we add a Repeater control to display any validation errors found. We use BootStrap framework for the design.

First, let's check the server-side validations. Best practice is to validate on the server no matter how well written our client-side validations are written, as client-side validations is dependent on so many variables. To validate the inputs, we call the function ScissorValidations.Validator.Validate(), which accepts an instance of the Employee class (can be a new or existing instance) and the mapping definition Dictionary<String, Validator.FieldMap>.

The fieldMapping variable defines how the class properties map to the page controls. Each element in the map holds the name of the property, and the FieldMap, which holds the page control as well as it's value to be validated. The Validate() function returns all validation results found, which can be used as a data source for a repeater control, for example. If none are returned, then it means that the inputs passed validation based on the defined rules, and can now be safely saved.

As a shortcut, it is also possible to let the Validate() function auto-assign the validated values directly into the data class instance that was passed. This is to minimize the need to assign and perform any casting needed which were already done internally during the validation process. Note that this only passes any values deemed valid by the validation process. To enable this functionality, set the global setting ScissorValidations.Validator.Settings.CopyValuesOnValidate to true. For ASP.NET, this can be set during the ApplicationStart() event in global.asax as below.

Now for client-side validations. Scissors Validation Framework acknowledges that client-side validations can have many strategies, be it custom Javascript validations or other established frameworks like BootStrap Validations. Due to this, it is normally advisable to implement one's own implementor, which implements the IValidationImplementor interface. For simplicity, let us use a built-in implementor already in the framework named BootstrapValidationImplementor, found in the ScissorValidations.ValidationImplementors namespace. This implementor uses simple validation configurations based on the BootStrap Validation library.

To wire up the page input controls with the required attributes to work with BootStrap Validations, we use the function InitializeClientValidators(), which accepts another fieldMapping variable, but using a slightly different Dictionary definition. Additionally, generic types need to be defined for the data class type containing the validator attributes we need, as well as an implementor class like BootstrapValidationImplementor.

Similar to the fieldMappings on the server-side validation, the client-side requires mapping the data class properties to their corresponding page input control, but is simpler as it only requires the property name and the target control instance.

Shown in the top half is the plain BootStrap-designed row for the FirstName field. This is how a field would typically look like before the client-side validators are added. After the InitializeClientValidators() function has been called and the FirstName field has been found to contain a validator attribute, then the framework uses the provided implementor class to decorate the input tag with the necessary HTML attributes as required by BootStrap Validations library. The bottom half in the snippet above shows the new attributes added to the input tag with the rules for this field's validations.

In case the built-in implementor is not exactly right to your needs, or you are using a totally different client-side validation strategies, you may use your own implementor by implementing the IValidationImplementor interface. The snippet below shows the contents of the BootstrapValidationImplementor class, which you may base your own off of (each function handles the insertion of required attributes depending on their validator types).

And that sets up both client-side and server-side validations on an ASP.NET page.

There will be more validator attributes to be added in the future, as well as a few more implementors. If you like what you see and wish to make some suggestion, do leave a comment. To get the codes, you may download it from GitHub.

33 comments:

  1. Thank you very much for sharing the framework info that will be much helpful for me to have something good regarding the validation of code.
    Emma Charlotte | TheAcademicPapers.co.uk

    ReplyDelete
  2. Thanks for sharing this valuable information this really help me a lot. Great Post Download Androdumpper apk Whatsdog apk Latest Version Test dpc Apkpure

    ReplyDelete

  3. thanks for your details it's very use for my works web design company in velachery

    ReplyDelete

  4. Very nice blog, Thanks for sharing grate article.
    You are providing wonderful information, it is very useful to us.
    Keep posting like this informative articles.
    Thank you.

    From: Field Engineer
    Cloud Systems Administrator

    ReplyDelete
  5. It’s very helpful for us, thank you so much for sharing such an amazing article. Visit Ogen Infosystem for top Website Designing and PPC Services in Delhi at an affordable price.
    Website Designing Company in Delhi

    ReplyDelete
  6. pgslot ซึ่งเกมคาสิโนออนไลน์เกมนี้เป็นเกมที่เรียกว่าเกม สล็อตเอ็กซ์โอ คุณรู้จักเกมส์เอ็กซ์โอหรือไม่ 90% ต้องรู้จักเกมส์เอ็กซ์โออย่างแน่นอนเพราะในตอนนี้เด็กนั้นเราทุกคนมักที่จะเอาก็ได้ขึ้นมา สล็อต เล่นเกมส์เอ็กซ์โอกับเพื่อนเพื่อนแล้วคุณรู้หรือไม่ว่าในปัจจุบันนี้เกมส์เอ็กซ์โอนั้นกลายมาเป็นเกมซะลอสออนไลน์ที่ให้บริการด้วยเว็บคาสิโนออนไลน์คุณสามารถเดิมพันเกมส์เอ็กซ์โอกับเว็บคาสิโนออนไลน์ได้โดยที่จะทำให้คุณนั้นสามารถสร้างกำไรจากการเล่นเกมส์เดิมพันออนไลน์ได้เราแนะนำเกมส์ชนิดนี้ให้คุณได้รู้จักก็เพราะว่าเชื่อว่าทุก

    ReplyDelete
  7. UI UX Design is becoming popular these days. UI Design and Tools is a 42-hour course where you learn the nuances of User Interface Design and the relevant tools employed by the industry. 
    This was a very brief and beautifully written blog. Even I came across certain websites that offer Design Course In Chennai: Visit design courses in chennai
    Hope to revisit soon!
    Visit design courses in chennai

    ReplyDelete
  8. The world has become increasingly interconnected and complex. As a UX designer, it is crucial to develop and refine skills to understand and address rapid changes in the environment and behavior of the consumer.
    If you are willing to learn more about UI Design and tools

    blockchain online training

    Digital journalism course

    ReplyDelete
  9. Once you’ve found your perfect freelancer, most of the hard work is over. The next step is to familiarize yourself with this new business software tool, and set it up in a way that will help you accomplish important business objectives.A good CRM will help you automatically log and act on email conversations with clients and prospective clients. for more info visit https://www.everlance.io/

    ReplyDelete
  10. In the “Page Setup” dialog box, ensure “Layout” tab is on. Choose “Right-to-Left” in “Section direction” field. Then click “Line Numbers” button and follow instructions in tip 1 to set line number options and click “OK” to save the change.For More Information visit adding line numbers in word

    ReplyDelete
  11. In this comparison of Dalstrong vs Shun, I dive deeper into the similarities and differences between these iconic knives. You’ll learn how they stack up in terms of options, design, performance, price, and more.

    ReplyDelete
  12. 추첨 방식은 로또와 비슷하나 확률은 그에 비해 극악인데 2012년 이전까지는 1~49 사이의 숫자 5개와 1~42 사이의 숫자 하나(이 숫자가 새겨진 볼 이름이 파워볼이다.)를 맞히면 1등을 할 수 있었으나 2012년 이후로는 1~69 사이의 숫자 5개, 1~26 사이의 파워볼 숫자 하나를 맞혀야 한다.

    ReplyDelete
  13. 스웨디시 마사지는 여러 아시아 국가, 특히 한국에서 사용되는 고전적인 마사지 기술입니다. 앞서 언급한 많은 이론들은 마사지 부위에 더 많은 압력이 가해질수록 마사지가 더 효과적이라는 생각에 근거합니다. 또한 스웨디시 마사지에는 신체 이완, 통증 감소, 통증 감소, 혈류 개선, 순환 개선, 이완 개선, 근육 긴장 감소 등 여러 이점이 있습니다.

    ReplyDelete
  14. The good news is that our officers are all physically fit. This helps them because their residential security in UKwork may involve lots of walking, running, scaling up walls, and even carrying their clients should matters get worse in the process of protecting them. They can also stand for hours while on duty and may be involved in a fight with those trying to attack the clients.

    ReplyDelete
  15. We provide our clients with the best close protection in London, which includes pathfinding, and convoy security chauffeur
    security services in high-risk areas. We have a team of experts who have operated in complex or high-threat environments for many years. They greceive et regular training so they can handle any situation that arises whilste ensuring the unique need of each client is complied with.

    ReplyDelete
  16. What replaced the Stardust PGSLOT Casino?
    Closure and demolition. On January 3, 2006, Boyd Gaming announced plans to close the Stardust within a year and replace it with a mixed-use project called Echelon Place, scheduled to open in 2010.

    ReplyDelete
  17. Making a good initial mandspeoplesystem marks and spencer employee login impression is critical for successful acculturation. Having a stress-free way to handle the transactional side of onboarding (completing forms, etc) eases anxiety and reduces distractions.

    ReplyDelete
  18. There are many brands of sporting goods that we need. From Good-quality exercise equipment to the usual quality, from an expensive price to the ordinary, from which considers security to the functional users only. From the telldickssportinggoods-smg-com various types of exercise equipment that available, we can determine which are appropriate to our needs.

    ReplyDelete
  19. With extremely tight tolerances and strict quality control, all Hornady 6.5 Creedmoor ammunition promises superior lot-to-lot consistency. From the bullet seating to the optimal charges and velocities, the 6.5 Creedmoor is designed to live up to company founder, J.W. buy Hornady 6.5 creedmoor online from our store.

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. การพนันเป็นหนึ่งในรูปแบบความบันเทิงที่ได้รับความนิยมมากที่สุดบนเว็บ เว็บพนันอันดับ 1 นักพนันมากมาย เป็นที่ทราบกันดีว่า dafabet345 เป็นเว็บไซต์การพนันออนไลน์ที่ได้รับความนิยมสูงสุด และได้เปิดให้บริการมาหลายปีแล้ว จากเมื่อก่อนอาจจะยังไม่เป็นที่นิยม ไม่ว่าด้วยเหตุผลใด ณ ปัจจุบัน เว็บพนัน dafabet345.

    ReplyDelete
  22. หากคุณเป็นคนประเภทที่ชอบเล่นสล็อตแมชชีน แต่คุณไม่ต้องการจัดการกับกองควอเตอร์ คุณจะรัก สล็อตแมชชีนโบนัส นี้ เครื่องเล่นเกมนี้มาในรูปแบบของโบนัสเกมสล็อตแมชชีนที่ออกแบบมาเป็นพิเศษเพื่อทำงานร่วมกับเกมคาสิโนที่คุณชื่นชอบ เนื่องจากสล็อตโบนัสให้ความตื่นเต้นและความตื่นเต้นมากกว่าเมื่อเทียบกับสล็อตมาตรฐาน มันจึงคุ้มค่าที่จะลอง สล็อตโบนัสเป็นวิธีที่ยอดเยี่ยมในการทำเงินพิเศษ ในเวลาอันสั้น คุณอาจจะเตรียมตัวเองให้พร้อมสำหรับการพนันไปตลอดชีวิ

    ReplyDelete
  23. ราคาบอล ไหลหรือราคาไหลเป็นคำที่รู้จักกันดีในอุตสาหกรรมการพนันฟุตบอลออนไลน์ โดยเฉพาะนักพนันบอลออนไลน์ทุกคนมักจะรู้จักคำนี้ค่อนข้างลึกซึ้ง เพราะหากมองดูการไหลของราคาบอล ก็สามารถวิเคราะห์ในการเลือกทีมที่จะเดิมพันได้ แต่มันจะถูกต้อง 100% เสมอ เพราะบางครั้งราคาก็สามารถหลอกผู้เล่นได้เช่นกัน หรือที่เรียกว่า ดังนั้นบางครั้งจึงไม่สามารถวิเคราะห์เฉพาะการไหลของราคาเพียงอย่างเดียวได้

    ReplyDelete
  24. ตามปกติแล้วคะแนนประตูของกีฬาฟุตบอลทั่วไป ปกติถ้าลง 2 ลูกจะเรียกว่าสกอร์ต่ำ แต่ถ้าเกิน 3 ลูก หมายถึง สกอร์สูง แต่จำไว้เสมอว่า คะแนนเดิมพันคือ 2.5 เมื่อยิงได้ 2 ลูก ให้ดูที่ เวลาที่เหลือ ว่าควรจะเติบโตสูงขึ้นหรือไม่ มาตัดสินใจกันอย่างเด็ดขาด บทความนี้เพื่อทำความรู้จัก แทงบอลสูงต่ำยังไง ฟุตบอลให้มากขึ้น เพราะหาเงินจากการพนันได้ง่าย ๆ.

    ReplyDelete
  25. If you live in a major city, then you have likely heard of license plate reader blocker. These cameras have been placed at various intersections throughout the city and have caused quite a stir. The reason these cameras have been in the news so much is that they have the ability to track a person and their driving habits.

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. The Wavy Chocolate Bars are more popular than ever. Everyone wants to get in on the trend, but not many know where to start. This blog will show you how you can get started making wavy chocolate bars. It will take you through the steps you need to take to get your shop up and running. company is one of the first producers of chocolate bars that has used a 3D printing machine in the production process. This blog will cover the different aspects of using 3D printers in the production process.

    ReplyDelete
  28. awesome article, it's miles especially beneficial! I quietly commenced in this, and i am becoming more acquainted alongside amid it higher! Delights, refrain take leisure motion greater and tallying superb! Download Microsoft Office 2013 With Crack

    ReplyDelete
  29. You delivered such an exceptional piece to retrieve, giving every difficulty enlightenment for us to attain records. thanks for sharing such information once us because of which my several concepts had been cleared. Office Timeline Plus Activation Code

    ReplyDelete
  30. thank you for an tempting blog. What else can also I attain that form of data written in the sort of answer entre? i've an conflict that i am just now full of animatronics upon, and i've been upon the lookout for such info. Eid Mubarak Wishes

    ReplyDelete