Home > Default > What is your strategy for form validation when using MVC pattern?

What is your strategy for form validation when using MVC pattern?

October 11Hits:0
This is more of a general discussion topic and will not necessarily have a correct answer. I'm using some of the Flex validator components in order to do form validation, but it seems I'm always coming back to the same issue, which is that in the world of Flex, validation needs to be put in the view components since in order to show error messages you need to set the source property of the validator to an instance of a view component. This again in my case seems to lead to me duplicating the code for setting up my Validators into several views. But, in terms of the MVC pattern, I always thought that data validation should happen in the model, since whether or not a piece of data is valid might be depending on business rules, which again should be stored in the model. Also, this way you'd only need to write the validation rules once for all fields that contain the same type of information in your application.
So my question is, what strategies do you use when validating data and using an MVC framework? Do you create all the validators in the views and just duplicate the validator if the exact same rules are needed in some other view, or do you store the validators in the model and somehow reference them from the views, changing the source properties as needed? Or do you use some completely different strategy for validating forms and showing error messages to the user?


Thanks for your answer, JoshBeall. Just to clarify, you would basically create a subclass of e.g. TextInput and add the validation rules to that? Then you'd use your subclass when you need a textinput with validation?
Anyway, I ended up building sort of my own validation framework. Because the other issue I had with the standard validation was that it relies on inheritance instead of composition. Say I needed a TextInput to both check that it doesn't contain an empty string or just space characters, is between 4 and 100 characters long, and follows a certain pattern (e.g. allows only alphanumerical characters). With the Flex built in validators I would have to create a subclass or my own validator in order to meet all the requirements and if at some point I need another configuration (say just a length and pattern restriction) I would have to create another subclass which duplicates most of the rules, or I would have to build a lot of flags and conditional statements into that one subclass. With the framework I created I can just string together different rules using composition, and the filter classes themselves can be kept very simple since they only need to handle a single condition (check the string length for instance). E.g. below is the rule for my username:
library["user_name"] = new EmptyStringFilter( new StringLengthFilter(4,255, new RegExpFilter(/^[a-z0-9\[email protected]\._]+$/i) ) );
<code>library</code> is a Dictionary that contains all my validation rules, and which resides in the model in a ValidationManager class. The framework calls a method <code>validate</code> on the stored filter references which goes through all the filters, the first filter to fail returns an error message and the validation fails:
(library["user_name"] as IValidationFilter).validate("testuser");
I only need to setup the rule once for each property I want to validate, regardless where in the app the validation needs to happen. The biggest plus of course that I can be sure the same rules are applied every time I need to validate e.g. a username.
The second part of the framework basically relies on Chris Callendar's great ErrorTipManager class and a custom subclass of spark.components.Panel (in my case it seemed like the reasonable place to put the code needed, although perhaps extending Form would be even better). ErrorTipManager allows you to force open a error tooltip on a target component easily. The subclass I've created basically allows me to just extend the class whenever I need a form and pass in an array of inputs that I want to validate in the creationComplete handler:
validatableInputs = [{source:productName, validateAs:"product_name"},
                     {source:unitWeight, validateAs:"unit_weight", dataField:"value"},
               {source:unitsPerBox, validateAs:"units_per_box", dataField:"value"},
                    {source:producer, validateAs:"producer"}];
The final step is to add a focusOut handler on the inputs that I want to validate if I want the validation to happen right away. The handler just calls a validateForm method, which in turn iterates through each of the inputs in the validatableInputs array, passing a reference of the input to a suitable validation rule in the model (a reference to the model has been injected into the view for this).
Having written this down I could probably improve the View side of things a bit, remove the dependency on the Panel component and make the API easier (have the framework wire up more of the boilerplate like adding listeners etc). But for now the code does what it needs to.

Read other 4 answers


Related Articles

  • What is your strategy for form validation when using MVC pattern?October 11

    This is more of a general discussion topic and will not necessarily have a correct answer. I'm using some of the Flex validator components in order to do form validation, but it seems I'm always coming back to the same issue, which is that in the wor

  • Release strategy for Future validity period- (Outline agreements)November 30

    Dear All, I want to create release strategy for Scheduling agreement.But I am creating the SA for furure validity period.In this case how the release strategy will triger. E.g Today 09.03.09 I create a Sceduling agreement and the validity period I ma

  • Validation of fields before emailing pdfNovember 30

    I have a form with a dropdown that when specific values are chosen from the dropdown they unhide certain fields.  Also, behind the submit button I have the following code.  The problem is that if the validation fails (for example if the dropdown valu

  • How to change the pattern dynamically in an Entity Level RegExp Validation?October 11

    Hi, In entity attribute level, i gave a Regular Expression Validation. In validation i hardcode the pattern like [A-Z a-z]{0,25}. Is it possible to assign the Pattern of RegExpValidationBean in Entity from a property file or a EL for getting the patt

  • XML Validation failingOctober 11

    Hi, I have XSD file, My application generates xml file dynamically. When validating my xml with the XSD i am getting below exception messages.please find the code of XSD amd XML files. org.xml.sax.SAXParseException: cvc-pattern-valid: Value '300' is

  • Validator not returning the results.November 30

    Hi All, I have extended the StringValidator class in order the create a username validator. When a new user is being registered, the username is validated on the server to check if it exists. The code is not throwing errors and i know the remoteobjec

  • Regarding error in xml validation in 7.1November 30

    Hi I am getting following error in xml validation in 7.1 my scenario is IDOC TO FILE from idoc DATE_TIME field comes with 14 digits. In the target I created DATE_TYPE field with data type "dateTime" and in details column pattern section I mentio

  • Validating UNC PathsNovember 30

    Consider the following PowerShell script: while ($Path -eq $null -or $Path -eq '') { $Path = Read-Host "Enter a local or UNC path" if ((Test-Path $Path -PathType Container) -and ((Get-Item $Path).PSDrive.Provider.Name -eq "FileSystem")

  • BPM process to manage business data vs Business Data in RDBMSOctober 11

    Hi all, I have so far seen BPM as a pure business process tool rather than a data management one even thought BPM provides for managing data. If in case, we have a nice Business Process, which also collects plenty of related business data, what would

  • ORPAS:How the percentages in curve profile and resulting buy periods defineNovember 30

    Dear All, Am new to RPAS Assortment planning application,and I have a query in Assorment planning Maintain Curve Profiles The percentages which are entered in maintain curve profiles will change in resulting buying periods If the length of the buying

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.706 s.