Form Validation

MiServer is Dyalog's APL-based web development framework
Post Reply
Gantois
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Form Validation

Post by Gantois »

Good morning Brian!

I have doubt about how to perform validation methods in a form before call a page declared in "action".

At first I created a scripted file (login.dyalog) with its validation methods within, using action="login.dyalog" (form tag) and changing to action="abc.dyalog" after validation. I have had some problems to call this page without pressing submit button again.

By other side I read about onSubmit event in a form tag to call validation function in java script before execute the page declared in "action", but I would like to use apl functions/methods.

What would be the right way? Certainly I will use it in almost all my forms. Could you help me with an example?

Thanks,
Marco
User avatar
Brian|Dyalog
Posts: 120
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: Form Validation

Post by Brian|Dyalog »

Hi Marco!

If I understand your question, I think there are a couple ways to accomplish what you're trying to do.

One way is to use a <meta> tag in the response's <head> to redirect the user to abc.dyalog. I've coded an example of this below.

Another technique would be to use AJAX and attach a 'click' handler to the submit button. MiServer's AJAX interface (we call it APLJAX), can conditionally send a JavaScript snippet to redirect to abc.dyalog.

I hope this helps!

Code: Select all

:Class login : MiPage

⍝ define the variable to bind the form's input elements to
    :field public uid←''
    :field public pwd←''
    :field public submit←''

    :include #.HTMLInput

    ∇ r←Render req;f
      :Access public
      r←''
      :If submit≡'Login' ⍝ did the user press the submit button?
     
          :If uid pwd≡'marco' 'pass' ⍝ validate the credentials
          ⍝↓↓↓ this inserts a <meta> element into the response's <head> element
          ⍝    which will redirect to abc.dyalog after pausing 0 seconds
              req.Meta'http-equiv="refresh" content="0;URL=/abc.dyalog"'
              :Return
     
          :Else ⍝ credentials did not validate
              r,←BRA'*** Invalid login!'
              uid←pwd←submit←'' ⍝ reset the form variables
          :EndIf
      :EndIf
      r,←'h2'Enclose'Please login:'
      f←BRA'Userid: ','uid'Edit'' ⍝ uid input element
      f,←BRA'Password: ','pwd'Password'' ⍝ pwd input element
      f,←'submit'Submit'Login' ⍝ submit button
      r,←req('post'Form)f ⍝ wrap inputs in a form
    ∇
:EndClass
Gantois
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: Form Validation

Post by Gantois »

Brian,
I used your first recommendation <meta> tag with success.
Thanks again,
Marco
Gantois
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: Form Validation

Post by Gantois »

Hi Brian!

I am using some txt files typed in notepad++ editor in my MiSite as the following: ex: html←#.Files.GetText req.Server.Root,'Styles\abc.txt'
Although the characters typed on that editor are visually right, some of them are changed on web page. Could you help me in this case? (My APL is 13.2 64 Unicode)

Thanks,
Gantois
User avatar
Brian|Dyalog
Posts: 120
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: Form Validation

Post by Brian|Dyalog »

Hi Gantois!

Without more information, like what specifically looks different :), I can guess that you've got some characters in your file that are known as HTML entities - these are reserved characters in HTML.

Try using #.HTMLInput.HtmlSafeText to convert your text to a "safe" format.
      html←#.Files.GetText #.HTMLInput.HtmlSafeText req.Server.Root,'Styles\abc.txt'


I hope this helps!
/Brian
Gantois
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: Form Validation

Post by Gantois »

Hy Brian,

Sorry, but I am learning miserver, html, css and OOP together, and I’m trying to practice English too … perhaps I will be crazy :) … but I am motivated and having goods results in my site, with your help of course.

Following more information about my problem:
In some cases I am using notepad++ editor to type txt files (html) as part of my MiServer pages, for example: welcome page and .txt files with some blocks of html. Every characters are recognized by notepad editor (and apl editor too) but after using #.Files.GetText or #.Files.GetText #.HTMLInput.HtmlSafeText, some characters are not recognized by browser. Examples:

Before: After:
Preparação Preparação
vários vários

Thanks,
Marco
User avatar
Brian|Dyalog
Posts: 120
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: Form Validation

Post by Brian|Dyalog »

Hi Marco!

It sounds like the file is in UTF-8 format and #.Files.GetText only reads simple ASCII or ANSI files. So, there are a couple of possible solutions:
  • If possible, save the file as ANSI. You'll find that under the Encoding menu item in NotePad++
  • Use ⎕SE.UnicodeFile.ReadText to read the file.
I hope this helps!
/Brian
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Form Validation

Post by PGilbert »

Hello Marco, you can use .Net to read your files back into your workspace. If your file is saved in Unicode the following can be used:

Code: Select all

⎕USING←',mscorlib.dll'
System.IO.File.ReadAllText(⊂,file_name)


If you are using the default encoding on your computer, the following can be used:

Code: Select all

⎕USING←',mscorlib.dll'
System.IO.File.ReadAllText((⊂,file_name),(System.Text.Encoding.Default))


In both cases if there is an error, it can be retrieved with ⎕EXCEPTION.Message

You can read more about System.Text.Encoding here.

The file handle is guaranteed to be closed by this method, even if exceptions are raised.

Good luck
Gantois
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: Form Validation

Post by Gantois »

Hi Brian and PGilbert,

I tested all recommendations and all worked correctly

Thanks very much for your help

Marco Gantois
Post Reply