Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to validate a primaryKey on the client

    I've got a setup wherein I store files in a database, and I can interact with these in my DataSource, fileDS. Users are allowed to delete files from this database.

    I'm having a problem if a user deletes a file, and then another part of the code tries to access that file through a saved entry in another database (accessed through another DataSource jobDS). Entries in jobDS are valid even if the file they reference has been deleted, but it is invalid for them to try to use that deleted file again.

    I have a custom CanvasItem which displays these files as a StaticTextItem. The value for the CanvasItem contains the filename for display purposes and the ID for later retrieval. I'm trying to find a way to add a validator for my CanvasItem which can validate the IDs if the user tries to use it.

    What I have is this:

    Code:
               validators: [
                  {
                     type:"custom",
                     condition:function(item,validator,value,record){
                        if(value == null) return true;
                        var valid = false;
                        fileDS.fetchRecord(value.ID,function(dsResponse,data,dsRequest){
                           valid = data.length > 0;
                        });
                        return valid;
                     },
                     errorMessage:"File not in database!"
                  }
               ],
    but this isn't working because the fetchRecord is executed asynchronously. Is there some other way I can validate an ID?

    1) Is there a way to do a synchronous call on the client (like the above)?
    2) It doesn't make sense that a server validator would live in my fileDS since its own primaryKey IDs are obviously valid.
    3) It doesn't make sense for a validator to be in my jobDS since people will want to inspect old entries even if the file used to create them has since been deleted.


    Now obviously I can throw an error later where I try to use the file on the server. I'm just wondering if there's a way to do this in a regular validator since it seems like such a simple thing to want to validate.
    Last edited by nitroamos; 15 Jul 2014, 10:00. Reason: accidental posting before finishing

    #2
    Validation errors are raised when attempting to save data. This sounds like an error that comes up when data is being displayed and the validation system should not be used for this kind of thing.

    It seems like your CanvasItem can just detect the present of the file via an ordinary DataSource fetch and show some kind of error display (again *not* a validation error) when the file does not exist.

    Comment


      #3
      In many regards, I'm using SmartClient software for purposes it was not designed for!

      I like your validation system because it's nice and clean and I'm not a fan of popups. I'll see what I can come up with.

      Comment


        #4
        For future googlers, here's what I ended up doing:

        Code:
                   validators: [
                      {
                         type:"custom",
                         condition:function(item,validator,value,record){
                            if(value == null) return true;
                            fileDS.fetchRecord(value.ID,function(dsResponse,data,dsRequest){
                               if(data.length < 1){
                                  var vm = dsRequest.validatingItem.form.valuesManager;
                                  vm.setFieldErrors(dsRequest.validatingItem.name,
                                                    "File not in database!",true);
                               }
                            },{validatingItem:item});
                            return true;
                         }
                      }
                   ],
        The function "validate()" incorrectly returns true when the user hits "submit" so my code keeps going to subsequent steps, but the error will appear before the user even notices there's problem.

        Comment

        Working...
        X