Ed Leafe wrote:
> On Mar 27, 2010, at 3:40 PM, Ricardo Aráoz wrote:
>
>   
>> There's a couple of things I want to do though and haven't found how to.
>> a) I'd like to do both, field validation and record validation. But I'd
>> like that when the field validation fails to set the corresponding edit
>> window object's foreground colour to red and let the user continue editing.
>>     
>
>       You should override the form's default behavior on failing field 
> validation. That method is onFieldValidationFailed(), and is passed the 
> control reference, the DataSource, the DataField, the value and the error. 
> This method by default displays the error in the status bar and then re-sets 
> focus to the control in question. If the control is the edit field, you could 
> set the ForeColor and simply return, so that the focus remains wherever the 
> user placed it.
>   

I found out a little problem here.
The field turns red and black correctly on field validation, and it's
exactly what I wanted. But if I leave the wrong value and click on the
save button the record is not saved (that's correct) but no error
message from the record validation is displayed. On the basis that field
and record validation should be orthogonal I think there's something wrong.
I know it is the code run by the field validator because when I comment
out the validateField() method of 'recipes.py' then the error window
comes up as it should.
In case it is my code, here it is :

----------------------------------------------------------------------------
class FrmRecipes(FrmBase):

    def initProperties(self):
        self.super()
        self.NameBase = "frmRecipes"
        self.Caption = "Recipes"
        self.SelectPageClass = PagSelectRecipes
        self.BrowseGridClass = GrdRecipes
        self.EditPageClass = PagEditRecipes


    def afterInit(self):
        if not self.Testing:
            # Instantiate the bizobj and register it with dForm, and
then let the
            # superclass take over.
            app = self.Application
            bizRecipes = app.biz.Recipes(app.dbConnection)
            self.addBizobj(bizRecipes)
        self.super()


    def onFieldValidationFailed(self, ctrl, dataSource, dataField, val,
err):
        ctrl.ForeColor = 'red'
        return


    def onFieldValidationPassed(self, ctrl, dataSource, dataField, val):
        ctrl.ForeColor = 'black'
        return
----------------------------------------------------------------------------

----------------------------------------------------------------------------

class Recipes(Base):
   
    def initProperties(self):
        self.super()
        self.Caption = "Recipes"
        self.DataSource = "recipes"
        self.KeyField = "id"

        # Setting the DataStructure explicitly here is optional, but
recommended as
        # it will keep Dabo from interpreting field types from the
backend every
        # time. It lets you specify what types you expect which fields
to be. Also,
        # this information is used in self.setSQL() to add the fields to
the query.
        # (field_alias, field_type, pk, table_name, field_name, field_scale)
        self.DataStructure = (
                ("id", "I", True, "recipes", "id"),
                ("title", "C", False, "recipes", "title"),
                ("subtitle", "M", False, "recipes", "subtitle"),
                ("ingred", "M", False, "recipes", "ingred"),
                ("proced", "M", False, "recipes", "proced"),
                ("date", "D", False, "recipes", "date"),
                ("image", "C", False, "recipes", "image"),
        )       

        # Use the DefaultValues dict to specify default field values for
new
        # records. By default DefaultValues is the empty dict, meaning that
        # no default values will be filled in.
        #self.DefaultValues['<field_name>'] = <value_or_function_object>
        self.DefaultValues['date'] = datetime.date.today

        # Default encoding is set to utf8, but if your backend db
encodes in
        # something else, you need to set that encoding here (or in each
        # bizobj individually. A very common encoding for the Americas and
        # Western Europe is "latin-1", so if you are getting errors but are
        # unsure what encoding to pick, try uncommenting the following line:
        #self.Encoding = "latin-1"
   

    def afterInit(self):
        self.super()
       

    def setBaseSQL(self):
        # Set up the base SQL (the fields clause, the from clause, etc.) The
        # UI refresh() will probably modify the where clause and maybe the
        # limit clause, depending on what the runtime user chooses in the
        # select page.
        self.addFrom("recipes")
        self.setLimitClause("500")
        self.addFieldsFromDataStructure()


    def validateField(self, fld, val):
        if fld == 'title':
            return self.validateTitle(val)


    def validateRecord(self):
        msgs = []
        msg = self.validateTitle()
        if msg:
            msgs.append(msg)
        if not self.Record.date or self.Record.date <
datetime.date(1900, 1, 1):
            msgs.append('There must be a valid date.')
       
        if msgs:
            return ("The recipe '%s' could not be saved because:\n"
                    % self.Record.title.strip() + '\n'.join(msgs))
        return '' ## everything okay


    def validateTitle(self, val=None):
        oldVal = self.oldVal('title')    ## as it was after requery
        curVal = self.Record.title        ## as it is now
        if val is None:
            # val is only sent in validateField, and is the user-entered
value
            # not yet in the bizobj at all.
            val = curVal
        if oldVal.strip() and len(val.strip()) < 2:
            return 'Title too short..'


    def beforePointerMove(self):
        self.cancel()
----------------------------------------------------------------------------







--- StripMime Report -- processed MIME parts ---
multipart/alternative
  text/plain (text body -- kept)
  text/html
---
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/[email protected]

Reply via email to