Results 1 to 4 of 4

Thread: [Answers] Validation

  1. #1
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135

    [Answers] Validation

    Grids do not currently 'Inherit dictionary rules' - Form and Canvas Sections can (off by default, check the Attribute to enable).
    Your validation check will be evaluated when the data is saved to the table and the update rejected if the validation fails, but the error message will not be shown in the Grid.



    Have you tried just calling your validation routine from the 'Validation' of the Grid column (double-click column header to access Attributes) where the users input the filter commands?
    Use "{}" to pass the character value entered in the Grid column to your routine, e.g. valudf function in loaded script library or valudf.prg:
    Validation: valudf("{}")
    If the routine returns .F., the 'Validation error message' will be displayed and the column value will be blanked out (Add) or set to its previous value (Edit).



    The Validation attribute on the textbox itself expects an expression which will be evaluated to return .T. (valid) or .F. (invalid and Error message attribute contents will be displayed or default message).

    The 'Valid When' method on the Canvas Section is called when changes have been made to data in the Section and you move to another record. This allows you to do a 'batch' validation of more than one item in the Section in one go if you don't want to do each individually as you move between them.



    Validation is not called for each keypress. It is called when you attempt to transfer focus away from the textbox with tab, enter or mouse click.



    Q:
    When the mandatory field has the focus, how I can prevent the user moves leaving the field blank or enter an invalid data?
    A:
    You need to use the "onbeforeupdate" delegate to prevent the field from being left blank. As I mentioned in another thread, this is called when the user attempt to save the record.



    Both the "Valid when" and the "Before update" can return .f. to prevent the data being updated. Obviously you can provide the user with some feedback using Lianja.showMessage() or something similar.



    You need to check the "Mandatory input" attribute on fields. This will prevent the field being saved when it is empty. This does not appear be available for grid columns.
    For a grid you need to use the "Valid when" delegate which will be called when a row is going to be updated. Inside this delegate you can then check various logical conditions of data that has been input.
    If you want to reject the input then the delegate function should return .f. otherwise return .t. if all is ok.



    ​All topics in [Answers] alphabetically:http://www.lianja.com/community/show...ll=1#post12352
    Last edited by josipradnik; 2016-03-03 at 03:36.

  2. #2
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135
    Q:
    I'm using a Grid section and need to validate user input into two camps. Entries must be unique. The table has a unique PK field and 2 candidates for user input fields. The PK is defined with seqno () and auto incremented.
    1.'m using a Split Grid
    2. I understand that the entry / editing data is done in the form of split grid, but can not access the attributes of form in the split grid
    3. I tried the following:
    a) specify the "validation" in the Attributes dialog and nothing happens. Example: p_valid ("{}"). Where should I put the code p_valid () procedure?
    b) I can do it in a column of the grid? what event should I use? Where I write the procedure code? how do I do the procedure call?
    A:
    Grid Column Attributes are accessed by double-clicking on the Grid Column Header. These Attributes then apply to the Column in the 'Grid View' and also in the 'Form View' when you are using a Split Grid.
    The 'Validation' Attribute is a logical expression, evaluating to .T. (true) or .F. (false). So you can have a simple expression, or call a user defined function like p_valid("{}"). The "{}" here is to pass the current (character) value to the p_valid function as a parameter. You can also specify a custom error message to be displayed if Validation fails (evaluates to .F.).



    The p_valid function used here could be a standalone p_valid.prg script as in the screenshot below, or it could be a function inside a function library. The function library could itself be a standalone library (opened using SET PROCEDURE ... ADDITIVE) or the Page or Section custom delegate library you are using for event delegates, in which case it will be opened for you automatically.



    If it is a standalone prg, just click the + button at the bottom of the 'Files' in the Sidebar in the Apps Workspace and it will prompt you for the filename. The default file extension is '.prg'. Yes, it will be available for the whole App. When you Deploy, it will be deployed as a .dbo (pseudo-compiled) file.

    If you have lots of functions you want to use, you can put them in a function/procedure library (create this using the + button as before):



    Then use set procedure to <lib> additive in a delegate called before the function is required, for example in the app.load() or the app.ready() (or a delegate for the relevant Page or Section).



    Or, as in the screenshot above, you can add functions ('anotherfuncstill' here) in with the event delegates. The custom libraries for event delegates (created when you create your first delegate) are opened for you automatically; there is no need to use set procedure in this case.

    Pages and Sections can also have a 'Valid When' event delegate called after the field validation.

    I have added a function in with the App's event delegates, and in the 'Settings' here:






    ​All topics in [Answers] alphabetically:http://www.lianja.com/community/show...ll=1#post12352

  3. #3
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135
    Q:
    I plan to switch from natural PK to surrogate PK but need an easy way to specify and validate candidate key fields
    May I know how I can check for candidate key field uniqueness in a data entry section
    A:
    When the record is save: use the UNIQUE index on the field (works like Candidate in VFP).


    For field-level validation, let's assume you are using a VT, and don't have all the fields available: you could do an SPT (or another VT) SELECT when there are field records (
    select count(*) from table where <uniquefield> = "<tested value>" and <pkfield> # "<pkvalue of record being tested>"
    .

    Uniqueness is better checked at the database engine level

    Any validation done in the UI for uniqueness could be correct when the validation is evaluated but incorrect at the time the data is committed on the server.

    Autoincfields and/or the use of the seqno() can be used to guarantee uniqueness also as these are atomic operations.
    When using auto generated fields it is better to mark them as readonly in form and grid sections.



    ​All topics in [Answers] alphabetically:http://www.lianja.com/community/showthread.php?2717-Answers

  4. #4
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135
    Q:
    I'm tryng to use the classic "Browse" command
    I I wryte:
    Code:
    open database southwind
    use example
    browse field title:c="MR.Mrs,Miss,Mr",;
    last_name:w=10:V=Validate(),;
    first_name
    
    proc validate()
    
    endproc
    in the validate, how can I test the value just insert in the "last_name" field?
    if I test "last_value" I see the previous value..

    A:
    Pass "{}" to the validation proc.
    Code:
    valproc("{}")


    Q:
    I'm trying to do validation for a Textbox field on a canvas.
    Suppose I set Validation to !empty("{}") and Error to "Field must not be empty".
    1. When I press the tab key, the error message appears in the yellow error dialog, but the cursor moves to the next field. Is this expected behavior? I would expect the focus to be kept on the offending field.
    2. If the field is empty to begin with, tabbing through the field does not generate the error dialog. Is this expected behavior? The field fails the validation, so I would expect the error dialog to appear and focus to remain on the offending field.
    3. What is the recommended approach if the validation for a field has multiple components, requiring varying error messages for the field?
    4. Are there other preferred alternatives (different delegates, etc.) for doing field level validation?

    A:
    The Validation expression is checked on exiting the field when the field value has been modified.
    This expression can also be a call to a function/procedure/prg, so you could handle your varying error messages their, e.g.
    Validation: myfunc("{}")
    Code:
    //myfunc.prg
    param fld_value
    do case
    case fld_value = "something"
    ...
    You also have Section and Page level 'Valid when' delegates and the Section 'Before data update' delegate where data validation can be carried out:




    All topics in [Answers] alphabetically: https://www.lianja.com/community/sho...ll=1#post13748

    These answers are also systematized on the site "Lianja developer": https://lianjadeveloper.wordpress.co...ry/validation/

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Journey into the Cloud
Join us