TOPIC: __valid rules are working?
#82
__valid rules are working? 4 Years, 5 Months ago
Hi, I have tried to use a __valid rule to do the checks that I wanted to do in a trigger (see this post).

Rule_myFieldToCheck__valid:
Code:


function()
  {
  if (..some conditions are false..) {
    return false
    }
  return true
  }



When the __valid rule returns false, the field become red as I expected for an invalid value, but a following click on the 'New' button of the AccessControl (or a click on Current->Save to save the record) will store the record as if it was valid.

Is this normal? How can I check some conditions to validate or invalidate a field? I have tried with triggers, but as I explained in the other post I cannot rollback the transaction to undo the changes if the checks fail. I have tried now with the __valid rule, but it would not block a save in case that the field with __valid rule is invalid... Help! :(
 
 
Mauro
 
#83
Re:__valid rules are working? 4 Years, 5 Months ago
For now I have managed to resolve in this way: in the AccessControl, I define a validField rule with a code like the following:

Rule_VALIDATE_mytable:
Code:

function()
  {
  if .New?() is true
    {
    // IF WE ARE HERE, IT MEANS THAT WE WANT TO SAVE A NEW RECORD
    if (..some checks on other fields are false..)
      return "ERROR!"
    else
      ..do some update operations..
    }
  else
    {
    // IF WE ARE HERE, IT MEANS THAT THE RECORD HAS BEEN READ OR ALTERED
      ..eventually do something..
    }
  return ""
  }



I associate this rule to the accesscontrol with
something like:

validField: VALIDATE_mytable

Do you think this is a good solution? There is a better (or more elegant) way to do this?

I think the better way would be if I could do all in a trigger (i.e. if I could do a t.Rollback() in the trigger in case the checks fail). This would mean a real separation between database logic and the user interface logic. Instead, my solution involves a strict relationship between user interface and database (because I have to do some checks in a rule that is executed only by an AccessControl) and then it seems to me that this is not too different from the development logic of some applications like MS Access...

Post edited by: Mauro, at: 2006/03/02 17:13
 
 
Mauro
 
#85
Re:__valid rules are working? 4 Years, 5 Months ago
We use the same validField solution.

I agree this is not ideal because it is only in the user interface.

I think there are some problems with __valid rules but I can not remember the details.

I notice your comment says you are "updating" in the valid rule. This is usually not a good idea - rules should not have "side effects", they should only return a value. The reason is that you have no control over when rules are run since they are triggered by dependencies changing and only executed when their value is needed (lazily).

There are some Access Send's that you can use to do updating e.g. Access_BeforeSave

Post edited by: andrew, at: 2006/03/02 19:02
 
 
andrew
 
#86
Re:__valid rules are working? 4 Years, 5 Months ago
andrew wrote:
[...]I notice your comment says you are "updating" in the valid rule. This is usually not a good idea - rules should not have "side effects", they should only return a value. The reason is that you have no control over when rules are run since they are triggered by dependencies changing and only executed when their value is needed (lazily). From my experiments, it seems to me that that the validField rule always runs after a click on any button of the AccessControl (NEW,EDIT,FIRST,PREV,ecc.). Anyway I too thinked that doing some database updates in the rule was not too appropriate, then now I have moved these updates in a trigger.
There are some Access Send's that you can use to do updating e.g. Access_BeforeSave
Can you show me a little example of how to use this send? I call the AccessControl from within a book, therefore where I should catch this send? Do you think that this is a solution better than the trigger?

P.S.: but in a client/server app, the triggers run in the server or in the client?
 
 
Mauro
 
#87
Re:__valid rules are working? 4 Years, 5 Months ago
When client-server, triggers are done on the server.

To respond to Send's you need a Controller e.g.

Code:

Controller
{
Controls: (Access tmp)
AccessBeforeSave()
{
Alert("AccessBeforeSave")
}
}



(Whether it's in a book or not does not matter.)

The Send's are not documented in the current release. Here is some documentation that will be in the next release:

Access_BeforeRecord()
Whenever the record being displayed in the Access changes (including new records). Sends the record currently loaded in the Access.

Access_SetRecord(record)
When the Access sets its RecordControl's data. Sends the record being set.

Access_NewRecord()
When a new record is loaded into the AccessControl (user chooses the "New" button, or On_New method is called from the application).

AccessBeforeDelete(t)
Sent just before deleting a record in AccessControl. Sends the transaction to be used for the delete.

Access_Restore(new_record?)
When Access is about to re-read the current record from the database, discarding any changes to the record that hadn't been saved. Sends a boolean value indicating whether it was a new record or not.

Access_RecordChange(modified_fields)
When the record in the Access is changed (any of the fields are modified). Sends a list of the fields that were modified.

Access_AfterField(field, value)
When the user modifies the record in the Access. Sends the field that was modified, and the new value of the field.

AccessBeforeSave(t)
When the Access record is about to be saved, after the validation is done on the record. Sends the transaction to be used for the save.

AccessAfterSave(t)
After the record has been saved. Sends the transaction that was used for the save.

AccessAfterSaving()
After the record has been saved AND the transaction has been completed.
 
 
andrew
 
#91
Re:__valid rules are working? 4 Years, 5 Months ago
Thank you very much. Instead of using a mix of validation rules and triggers, do you think that is better if I move all the database logic in the AccessControl, and catching sends in a controller?
 
 
Mauro
 
#96
Re:__valid rules are working? 4 Years, 5 Months ago
We use a mixture of valid rules, Access methods, and triggers.

The problem with valid rules and Access methods is that they are in the user interface. Triggers are better because they will be done any time the table is accessed, from anywhere. But some things are hard or impossible to do just in triggers.

Recently I have been working on a project with Ruby and Rails. I really like how Rails puts all the validation and the before/after saving/loading stuff into a model class and all access to the data is through the model. I have some ideas on how we could do similar things in Suneido.
 
 
andrew