1

I need to validate a field as a Zip code and I want to use the FormItem-getter that was generated for the table I'm building a form for. There's no "GetZipCodeZipFormItem" FormItem getter being generated but I've noticed you can validate a Zip code with the Validator class. I would just add it manually with the my DataModification but I don't have a reference to the PostBackValueDictionary needed to get the value from the FormItem's control.

How can I validate this FormItem as a Zip code?

William Gross
  • 2,083
  • 2
  • 17
  • 35
Sam Rueby
  • 5,914
  • 6
  • 36
  • 52

1 Answers1

1

Assuming yourModObject.ZipCode is a string:

yourModObject.GetZipCodeFormItem(
    true,
    ( value, label ) => new EwfTextBox( value ),
    ( control, pbv, subject, validator ) =>
        validator.GetZipCode( new ValidationErrorHandler( subject ),
                              control.GetPostBackValue( pbv ),
                              true ).FullZipCode,
    value: "",
    validationList: yourDataModification )

Another way to do it is:

yourModObject.GetZipCodeTextFormItem(
    true,
    true, // allow empty
    value: "",
    additionalValidationMethod: ( subject, validator ) =>
        yourModObject.ZipCode = validator.GetZipCode( new ValidationErrorHandler( subject ),
                                                      yourModObject.ZipCode,
                                                      true ).FullZipCode,
    validationList: yourDataModification )

One drawback to the second approach is that the primary validation (i.e. the logic that stores a value in the mod object) is not aware that it's a ZIP Code that is being entered, so if, for example, your database field had a limit of nine characters (to accommodate a ZIP+4) and the user entered "12345-1234", primary validation would fail because the dash pushes the string over the length limit. You wouldn't even get to the additional validation method. This problem doesn't exist with the first approach.

William Gross
  • 2,083
  • 2
  • 17
  • 35
  • If I was able to use use `yourModObject.GetZipCodeTextFormItem` I wouldn't have to create the control myself, or have to create the validationErrorHandler passed with the subject that's given to me. There's an `additionalValidationMethod` optional parameter but it just passes me the label and a validator. What's the point of that? Why doesn't that just give me the value and if it fails my validation, just call `validator.NoteErrorAndAddMessage( )`? – Sam Rueby Sep 07 '12 at 00:37
  • 1
    @Sam.Rueby: I added a second code example showing how you could use the `additionalValidationMethod` to solve the problem, but as I said, there is a drawback to this approach. Also, the `additionalValidationMethod` does not give you the value as a parameter because you can easily retrieve it from the mod object as I show above, and because we designed this method for doing validations that involve multiple fields (maybe you need to validate that two different ZIP Codes match, for example). – William Gross Sep 07 '12 at 21:04