Validating HTML form data (COMatePLUS)
Posted: Mon May 04, 2009 9:17 pm
Hi,
a misleading title because this demo does not validate HTML form data!
However, it would be very straight forward to alter it to do exactly that!
What this demo does is prevent all HTML form submissions from any web-page loaded into the web-gadget - even pages which you subsequently navigate to! Altering this for validating form entry would actually entail simplifying this code somewhat!
NOTE that the code does not work for forms embedded within frames - the code would need extending slightly for that.
NOTE also that COMatePLUS version 1.0.4 is needed for this code to run correctly.
If you test with the PB forums URL, then navigate to a new topic and try to preview your entry. You should find that the submission is prevented.
a misleading title because this demo does not validate HTML form data!

However, it would be very straight forward to alter it to do exactly that!
What this demo does is prevent all HTML form submissions from any web-page loaded into the web-gadget - even pages which you subsequently navigate to! Altering this for validating form entry would actually entail simplifying this code somewhat!

NOTE that the code does not work for forms embedded within frames - the code would need extending slightly for that.
NOTE also that COMatePLUS version 1.0.4 is needed for this code to run correctly.
Code: Select all
IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"
;The following global linked-list holds the COMateObject objects representing the submit buttons.
;We use this list so that we can free these objects as and when appropriate.
Global NewList submitObjects.COMateObject()
Define browser.COMateObject
;-SUBMIT EVENT PROCEDURE.
;/////////////////////////////////////////////////////////////////////////////////
;The following is our event callback for our submit-button objects.
Procedure SubmitEventProc(Object.COMateObject, eventName$, parameterCount, *result.VARIANT)
If eventName$ = "onclick"
;Prohibit the submit action by simply returning a variant false value.
*result\vt = #VT_BOOL
*result\boolVal = #VARIANT_FALSE
MessageRequester("COMatePLUS", "Submit prevented!")
EndIf
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
;-LOCATE ALL SUBMIT BUTTONS FUNCTION.
;/////////////////////////////////////////////////////////////////////////////////
;The following function trawls the HTML DOM object attached to the given web gadget looking for form-submit buttons.
;For each such button we set up an event handler to catch the "onclick" event.
;We also add the object (as a COMateOBJECT) to the global list described above.
;Returns a count of such objects retrieved.
Procedure.i RetrieveSubmitButtonObjects(browser.COMateObject)
Protected result, numObjects, COMateObject, form.COMateObject, element.COMateObject
Protected forms.COMateEnumObject, elements.COMateEnumObject
If browser
forms = browser\CreateEnumeration("document\forms")
If forms
form = forms\GetNextObject()
While form
elements = form\CreateEnumeration("Elements")
If elements
element = elements\GetNextObject()
While element
If element\GetStringProperty("type") = "submit"
;We have a submit button object!
;We now set a global event handler with a return type of #COMate_VariantReturn because we wish to return a boolean value in some cases.
element\SetEventHandler(#COMate_CatchAllEvents, @SubmitEventProc(), #COMate_VariantReturn)
AddElement(submitObjects())
submitObjects() = element
result + 1
Else
element\Release()
EndIf
element = elements\GetNextObject()
Wend
elements\Release()
EndIf
form\Release()
form = forms\GetNextObject()
Wend
forms\release()
EndIf
EndIf
ProcedureReturn result
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
;-FREE ALL SUBMIT BUTTON OBJECTS.
;/////////////////////////////////////////////////////////////////////////////////
Procedure FreeSubmitButtonObjects()
ForEach submitObjects()
submitObjects()\Release()
Next
ClearList(submitObjects())
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
;-BROWSER CALLBACK.
;/////////////////////////////////////////////////////////////////////////////////
;The following is the event procedure for our browser object.
;We use this in order to rebuild our submit button objects list every time the user navigates to a new page.
Procedure BrowserEventProc(Object.COMateObject, eventName$, parameterCount)
If eventName$ = "DocumentComplete"
FreeSubmitButtonObjects()
RetrieveSubmitButtonObjects(Object)
EndIf
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
;Use one or the other URLs.
; URL$ = "C:\Pure basic - 4.3\COmatePLUS\Basic demos\SimpleForm.html"
URL$ = "http://www.purebasic.fr/english/"
#WebGadget = 0
If OpenWindow(0, 0, 0, 800, 600, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(#WebGadget, 0, 0, WindowWidth(0),WindowHeight(0), URL$)
browser = COMate_WrapCOMObject(GetWindowLong_(GadgetID(#WebGadget), #GWL_USERDATA))
If browser
;Set the 'global' event handler for the browser object.
;We wish to trap the "DocumentComplete" event which means that we must use the DWebBrowserEvents2 out-going interface.
;In order to over-ride COMatePLUS' selection of 'connection point' we pass a pointer to the IID of the DWebBrowserEvents2 out-going interface
;within the second optional parameter.
If COMate_GetIIDFromName("DWebBrowserEvents2", @iid.IID) = #S_OK
browser\SetEventHandler(#COMate_CatchAllEvents, @BrowserEventProc(), 0, iid)
Else
MessageRequester("COMatePLUS -web demo", "IID for the DWebBrowserEvents2 interface was not found on this system!" + #CRLF$ + #CRLF$ + "Please contact the author of COMatePLUS.")
EndIf
Repeat
Event = WaitWindowEvent();
Select Event
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
browser\Release()
FreeSubmitButtonObjects()
Else
MessageRequester("COMatePLUS -web demo", "Couldn't create the browser object!")
EndIf
EndIf