Page Index Toggle Pages: [1] 2  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) Subform Validation (Read 2390 times)
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Subform Validation
Oct 24th, 2007 at 12:30am
Print Post Print Post  
I'd like to make a field required on a subform.  I've tried two methods:

1) Set the Restriction on the field to !.  This works sometimes, but it can be bypassed.  For example, it works when you try to navigate to different sub-record, but it fails to be enforced when you save the main form.

2) Programatically check the field in the subform's Form Exit event (code example below).  This leads to an infinite loop within Sesame that can I can only kill via the Windows Task Manager.

Code
Select All
NotifyForm(1)

If @IsBlank( txtField ) Then
	@Msgbox( "The field must be populated.", "", "" )
Else
	NotifyForm(-1) 



So where should subform validation occur?

EDIT: Maybe this question belongs on another thread, but related to the above, I'd like to use the FormDependentValue to recalculate my subrecord totals automatically.  However, that function appears to force a commit of the current subrecord, which I don't want to have happen if validation fails.  I'd like to add automatic re-totaling, but when I tried to add it by looping through the subrecord result set on the parent form's Change event, I couldn't add new sub-records programatically (as the loop would eliminate the uncommitted subrecord which I programatically created using @FormNewRecord).
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Validation
Reply #1 - Oct 24th, 2007 at 1:19am
Print Post Print Post  
MP wrote on Oct 24th, 2007 at 12:30am:
EDIT: Maybe this question belongs on another thread, but related to the above, I'd like to use the FormDependentValue to recalculate my subrecord totals automatically.  However, that function appears to force a commit of the current subrecord, which I don't want to have happen if validation fails.  I'd like to add automatic re-totaling, but when I tried to add it by looping through the subrecord result set on the parent form's Change event, I couldn't add new sub-records programatically (as the loop would eliminate the uncommitted subrecord which I programatically created using @FormNewRecord).


2.0.4 has several improvements for issues like this with FormDependentValue. We'll be putting that release out probably around the end of this month.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Subform Validation
Reply #2 - Oct 24th, 2007 at 1:58am
Print Post Print Post  
Hammer wrote on Oct 24th, 2007 at 1:19am:
2.0.4 has several improvements for issues like this with FormDependentValue. We'll be putting that release out probably around the end of this month.

I'll keep an eye out for it.  In the meantime, my workaround was to check to see if the subset was currently in an Add state (which I could only do by seeing if the current position was greater than the record count).

Will 2.0.4 also address the validation issue?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Validation
Reply #3 - Oct 24th, 2007 at 11:56am
Print Post Print Post  
MP wrote on Oct 24th, 2007 at 1:58am:
Will 2.0.4 also address the validation issue?

It will get a couple of issues out of your way if you are using FormDependentValue.

For your overall validation routine, you will want a couple of pieces. You'll need a single level validation in the subform, for when they only save it. You'll also want a multi-level routine in the parent which does something like the following pseudocode:

PreventSave()
CheckSub()
If SubPasses
{
    CommitSub()
    CheckParent()
    If ParentPasses()
    {
       AllowParentSave()
    }
}
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Subform Validation
Reply #4 - Oct 24th, 2007 at 12:40pm
Print Post Print Post  
Hammer wrote on Oct 24th, 2007 at 11:56am:
For your overall validation routine, you will want a couple of pieces. You'll need a single level validation in the subform, for when they only save it. You'll also want a multi-level routine in the parent


1) Where would I put the single level validation to avoid the infinite loop problem that happens when I put it in the subform's Exit event?  (To be clear, this loop can happen for example when I try to navigate away from the invalid subform using the "Previous" (i.e. <) navigation button.)

2) What I'd like to avoid is duplicating my validation logic, which appears to be what you're suggesting via the implementation of CheckSub()
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Validation
Reply #5 - Oct 24th, 2007 at 12:47pm
Print Post Print Post  
MP wrote on Oct 24th, 2007 at 12:40pm:
1) Where would I put the single level validation to avoid the infinite loop problem that happens when I put it in the subform's Exit event?  (To be clear, this loop can happen for example when I try to navigate away from the invalid subform using the "Previous" (i.e. <) navigation button.)

Depends on which condition is causing the problem. When you click the navigation button, are you moving to the previous sub or the previous parent? Do you have any On Form Entry or On Element Entry programming that might be triggering when you click OK on the message box and return focus to the form? Are these form view or table view subforms?

Quote:
2) What I'd like to avoid is duplicating my validation logic, which appears to be what you're suggesting via the implementation of CheckSub()

I understand, but it may be necessary depending on the situation.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Subform Validation
Reply #6 - Oct 24th, 2007 at 1:40pm
Print Post Print Post  
Hammer wrote on Oct 24th, 2007 at 12:47pm:
Depends on which condition is causing the problem. When you click the navigation button, are you moving to the previous sub or the previous parent? Do you have any On Form Entry or On Element Entry programming that might be triggering when you click OK on the message box and return focus to the form? Are these form view or table view subforms?

Moving to the previous sub.
No.
Form view.

To re-create the issue in 2.0.3, I took the subform application from the Tutorial (which doesn't have any code) and added the following code to the Form Exit event of the LineItems form:

Code
Select All
NotifyForm(1)

If @IsBlank( Qty ) Then
	@Msgbox( "The Quantity field must be populated.", "", "" )
Else
	NotifyForm(-1) 


Now, when I go to Add data to the Invoice form, start doing data entry in the Subform, then press Previous, the infinite loop occurs.  I can also trigger the loop by simply clicking "Exit Add Data" from the tree menu.  (Oddly enough, if I leave the app for a period of minutes and then come back and click OK on the Msgbox, the loop stops.  But otherwise, I've found no way to exit the loop without killing the process.)
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Validation
Reply #7 - Oct 24th, 2007 at 1:43pm
Print Post Print Post  
MP wrote on Oct 24th, 2007 at 1:40pm:
Now, when I go to Add data to the Invoice form, start doing data entry in the Subform, then press Previous, the infinite loop occurs.  I can also trigger the loop by simply clicking "Exit Add Data" from the tree menu.  (Oddly enough, if I leave the app for a period of minutes and then come back and click OK on the Msgbox, the loop stops.  But otherwise, I've found no way to exit the loop without killing the process.)


It's probably a focus loop of some sort. I'll have Support get it to replicate in a testbed and let you know what we see.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Subform Validation
Reply #8 - Nov 14th, 2007 at 3:02am
Print Post Print Post  
Subform validation continues to give me fits.  Beyond what I've previously reported, here are some of the new gotchas I found today:

1) When I'm trying to validate the fields in a subform from a procedure in the parent form, I can't easily figure out if I should perform the validation in the first place.  The issue is that Sesame creates an uncommitted subform record that is accessible via FormFieldValue.  One should only bother to validate this form if there has been at least one edit to it.  With no edits, Sesame dumps the uncommitted record (as it should) when one changes the parent record.  So how do you determine from the subform if the parent form has been edited?  Well, you can determine if you are dealing with a new, uncommitted record by seeing if FormResultSetCurrentPosition is greater than FormResultSetTotal.  Then, the only thing I could think to do is to check the @LEN of all of the subforms fields.  I couldn't figure out how to do this using the metadata related Element commands, as those don't seem to be able to access the subform elements.  This is an ugly way to check if validation is necessary, but at least it works.

2) Restriction checks do not occur when using methods such as FormResultSetCurrentPosition.  This presents problems for the navigation buttons I created for the subform.  So while the restrictions are enforced by the Sesame provided navigation buttons, they are bypassed by my navigation buttons (thus causing me to manually implement validation on each navigation button).  However, since restriction checks are the only form of subform validation available when navigating with the Sesame provided navigation, one is then forced to have two separate (and thus confusing) methods of reporting validation errors to the user.

3) If a Restriction check fails for the current record while checking the value of a field in another record in the result set using FormFieldValue, the edit to the invalid field in the current record is simply discarded and the current record reverts to the original value.  Where this causes me a headache is in my automatic re-totaling of the subrecords.  I put this re-totaling in the On Form Change event of the parent form.  However, as this is called even when there are edits to the subform, invalid edits simply automatically unwind when the focus changes.  This is not an evil problem, but it is another confusing UI issue for end users.  Now in my app, bad edits may either trigger the generic Sesame Restriction dialog, a custom dialog, or simply automatically be undone.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Validation
Reply #9 - Nov 14th, 2007 at 3:34am
Print Post Print Post  
MP:

1. Try using the On Form Exit event instead of Restrictions to validate the subforms. This allows you to use the same validation method for both your buttons and for manual navigation.

2. Use the new FormDependentValue call to automatically total your subrecords onto the parent record.

3. Restrictions and such are not checked if you are using FormFieldValue. We assume that, if you are setting a value in code, you can validate it before doing so. Throwing a restriction message at the user when they may have no way to alter the value the programmer has decided to set will make them come after us with baseball bats. We hate that.  Smiley

BTW, you may want to hold off for just a bit as 2.0.4 will be available soon. This release improves the triggers on FormDependentValue and also fixes the focus loop you reported earlier so you can move between parents and subs without triggering loops.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Subform Validation
Reply #10 - Nov 14th, 2007 at 2:27pm
Print Post Print Post  
Hammer wrote on Nov 14th, 2007 at 3:34am:
3. Restrictions and such are not checked if you are using FormFieldValue. We assume that, if you are setting a value in code, you can validate it before doing so.

Although I implied it, I should have been more specific and said @FormFieldValue instead of FormFieldValue.  With a Get operation, it's clear that restriction checks should not come into play.  However, they do.  If you mark a field as required (!), delete the value in the field, and then have code that runs @FormFieldValue on a record that's not the current record in the subform, the delete will be undone and the original value will re-appear in the field.  I can live with the issue, but it's not proper.

Once 2.0.4 is released, I'll review the validate logic and report back my findings on this thread.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Validation
Reply #11 - Nov 14th, 2007 at 4:03pm
Print Post Print Post  
MP wrote on Nov 14th, 2007 at 2:27pm:
Although I implied it, I should have been more specific and said @FormFieldValue instead of FormFieldValue.  With a Get operation, it's clear that restriction checks should not come into play.  However, they do.  If you mark a field as required (!), delete the value in the field, and then have code that runs @FormFieldValue on a record that's not the current record in the subform, the delete will be undone and the original value will re-appear in the field.  I can live with the issue, but it's not proper.


If you are moving the current record in code, we don't really have much choice in the situation you describe. The conflicting instructions with Restrictions telling Sesame "Don't leave the record" and the programmer telling Sesame "You must leave the record" forces Sesame into a situation where no matter what it does it's partially wrong. In this case, it's making the safest choice it can by restoring the original valid data and moving the programmer to the record they expect to be on.

I suggest that, if this is an issue, before using @FormFieldValue to move to a different subrecord, validate the current subrecord with FormFieldValue. Then you can decide for yourself how you want to behave with regard to the invalid values before moving to a different record.

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Subform Validation
Reply #12 - Nov 14th, 2007 at 5:01pm
Print Post Print Post  
I think it was raised a few weeks ago that OnFormExit code does not work with buttons that advance records and you must put that code into Global Options as a SubRoutine?

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Subform Validation
Reply #13 - Nov 14th, 2007 at 5:56pm
Print Post Print Post  
Hammer wrote on Nov 14th, 2007 at 4:03pm:
If you are moving the current record in code, we don't really have much choice in the situation you describe. The conflicting instructions with Restrictions telling Sesame "Don't leave the record" and the programmer telling Sesame "You must leave the record" forces Sesame into a situation where no matter what it does it's partially wrong. In this case, it's making the safest choice it can by restoring the original valid data and moving the programmer to the record they expect to be on.

@FormFieldValue should not move the current record.  It should retrieve a value from a record in the result set, which may or may not be the current record in the result set.  There's nothing inherent about this type of operation that should force Sesame to "leave the record".  But that gets into a whole discussion about the internals of your result set management, which is outside of the bounds of this forum.

Anyhow, I appreciate the feedback.  Hopefully with 2.0.4 I can abandon the Restriction logic and go back to using Form Exit in the subform for validation.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Subform Validation
Reply #14 - Nov 14th, 2007 at 7:02pm
Print Post Print Post  
MP wrote on Nov 14th, 2007 at 5:56pm:
Anyhow, I appreciate the feedback.  Hopefully with 2.0.4 I can abandon the Restriction logic and go back to using Form Exit in the subform for validation.

I think you will be able to do so. Several of the changes in 2.0.4 were shaken out as a result of my needing to implement unusually rigid subrecord validation for a client in combination with needing to handle both automated and manual navigation and my app is working fine under 2.0.4 without restrictions.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send Topic Send Topic Print Print