On Smalltalk

thoughts on Smalltalk and programming in general…
  • Home
  • About
  • Good Books
  • My Squeak Image
  • Popular Posts

Multiple Field Validation Rules in Magritte

By Ramon Leon - February 25, 2007 under Magritte, Smalltalk

When working with Magritte, adding validation rules to descriptions is obvious and simple

descriptionStartDate
    ^MADateDescription new
        selectorAccessor: #startDate;
        label: 'Start Date';
        beRequired;
        addCondition: [:value | value > Date today];
        yourself

But adding a validation rule that depends on the values of several fields isn’t nearly as obvious and is also something that any decent business object needs quite often. Suppose in addition to the above description we have…

descriptionEndDate
    ^MADateDescription new
        selectorAccessor: #endDate;
        label: 'End Date';
        beRequired;
        addCondition: [:value | value > Date today];
        yourself

Suppose we want to enforce the #endDate being after the #startDate. We can’t add that rule to the #endDate description because we can’t reference the #startDate value. We need to add the rule in a place where we can ensure all single field data exists but before its written to the object from the mementos. We need to add a rule to the objects container description like this…

descriptionContainer
    ^(super descriptionContainer)
        addCondition: [:memento |
            (memento cache at: self descriptionEndDate) > (memento cache at: self descriptionStartDate)]
        labelled: ‘End date must be after start date’;
        yourself

This simply intercepts the container after it’s built, and adds a multi field validation by accessing the potential value of those fields. You get passed the memento for the object, and all the field values are in its cache, keyed by their descriptions. You simply read the values and validate them before they have a chance to be written to the real business object. Multi field validations are a bit more complicated than single field validations at the moment, but this pattern works well.

Tags: Magritte

Related posts
    at: "Using Magritte With Seaside";
    at: "Rails vs Seaside From a Java Developer";
    at: "A Smalltalk ActiveRecord using Magritte, Seaside, and Glorp";

1 Comment so far

  1. Using Magritte With Seaside on September 10th, 2007

    [...] posted about these previously. They are attached to your containers description which has access to all the [...]

Posting your comment.


  • Sponsors

  • Tags

    Databases General Linux Lisp Magritte Performance Profiling Programming Ruby Seaside Smalltalk Sql Squeak Updates
  • Categories

    • .Net (5)
    • Databases (9)
    • General (5)
    • Linux (2)
    • Lisp (3)
    • Magritte (2)
    • Programming (62)
    • Ruby (6)
    • Seaside (42)
    • Smalltalk (72)
    • Sql (2)
    • Stuff I Just Like (6)
    • Updates (7)
  • Blogs

    • (gem)Stone Soup
    • Avi Bryant
    • Boris Popov
    • defmacro
    • Giles Bowkett
    • Goran Krampe
    • James Robertson
    • Lukas Renggli
    • Martin Fowler
    • Paul Graham
    • Ralph Johnson
    • Randal Schwartz
    • Vassili Bykov
    • Weekly Squeak
  • Favorite Tools

    • Apache
    • Cygwin
    • FireFox
    • Scriptaculous
    • Seaside
    • Squeak
    • Squeak Dev Image
    • Ubuntu Linux
    • WordPress
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org

Copyright © 2008 On Smalltalk