1. Active Member
Join Date
Apr 2008
Posts
23

## Re: Help with Computation

Originally Posted by aaron_w

What do all the arguments to initialize the float mean?

It looks to me like you've got a "number of digits after the decimal point" argument, a "decimal point separator" argument, and then something which you're passing a comma to - I assume thousands separator?
OK, I found the answer to that question ---

Join Date
Feb 2007
Posts
72

## Re: Help with Computation

Hello Arron

There is a whole wealth of information at: http://tectite.com/fmhowto/guides.ph...abfd09b1e813f8

The reading is short and to the point, which is what's great about formmail w/extras, you need to do the creation, formmail is only a tool. Instead of asking "what can formmail do?" you should be asking "what do I want it to do?" Chances are formmail w/appropriate mods can do whatever you need.

Enjoy exploring
Nelson

3. Active Member
Join Date
Apr 2008
Posts
23

## Re: Help with Computation

Hey Nelson ---

Thanks, I'm familiar with those and have scoured them

It's an amazingly capable little tool for a formmail script

I was more wondering about the specific capabilities of the computation module, and as far as I know, there's no doc page for that one yet ...

4. ## Re: Help with Computation

Hi Aaron,

Originally Posted by aaron_w
First off, what data types do we have?
Are there also integers? If so, how to initialize them?
Inside a computation, you have:
• bool
• int
• string
• float
Outside a computation, we're just dealing with HTML fields, and fields are just strings - there are no types.

So, that's why we import and export fields to and from the computations. In the process of importing, you can convert the field (string) to the type you're wanting it to be.

In the process of exporting you're converting the type to a string, and the case of floats, you can format them as you've seen.

In the case of exporting int's you can convert base (radix) and insert thousands separators:
Code:
export int    iVal1(8);       // octal, or base 8
export int    iVal2(10,',');  // base 10, with comma for thousands
How strongly typed is this little language?
It is strongly typed, but with sensible conversions (though I think some things need to be tightened too). You can add numbers and strings, and the strings are converted to the number type. But you can't add numbers and bools, for example.

You can't assign to an imported variable (field), unless it is also marked for export.

The most important thing is that you cannot use a variable without declaring it first.

Languages that don't do that, haven't learned the lessons from the languages created in the 1960's and 1970's.

And how do the error functions work?
Do you just go
if (something) { FMUserError('something') }
?
That's correct.

For FMFatalUserError and FMUserError you just pass a string.

For FMFatalError, there are 3 parameters: the string as above, a bool to specify whether to filter the message (you would generally want false for this), and a bool to specify whether to show the error to the user (person submitting the form).

BTW, these functions are actually defined in FormMail, and imported into the Computation.

Sorry, lots of questions, but this just opens up a whole can of worms for my mind wanting to know what is and isn't possible, since there doesn't seem to be much documentation on this, save for the sample and what's in the forums ...
Yes, sorry.

If you look around the site, you'll see we don't yet actively market the computation module. We offer it to solve specific problems people have (mostly Advanced Tempaltes).

We *will* be writing a manual as soon as we possibly can (and ramping up the marketing).

Also is it possible to put the "fmcompute1", ... fields into the .ini so as to hide your form computation logic from the world?
Yes!

This is best with PHP 5 because PHP 4 doesn't allow you to have an INI file value with newlines embedded - which makes your computation field unreadable.

5. Active Member
Join Date
Apr 2008
Posts
23

## Re: Help with Computation

Awesome!

Thank you.

I'm especially glad I can put these in the .ini file.

I can't think of why I'd want to at present, but can you define functions and/or import your own functions?

Of course, at a certain point, it may be a case of "why don't you just use PHP and do these things yourself?"

But I have been quite impressed by this little form handling framework and it does take some of the regular things you do with forms (error handling, input validation, confirmations, multi-pages, etc) much easier.

Aaron

6. ## Re: Help with Computation

Hi,

Before I respond to your questions, I just realized I made a mistake in the above (I really need to write the documentation ).

When importing fields, you *cannot* currently import into a specific type. Fields are strings, so you need to:
Code:
import string fieldname;
If you want, you can then convert to a specific type:
Code:
import string fieldname;
float fField;

fField = fieldname;
I'm not sure why the import-to-a-type was not implemented, but I'm sure I had good reason at the time: I think it was probably the problem of handling errors - what happens if you import an alpha into a float?

Often, users (and spammers) will put junk into fields.

However, I think it's an opportunity for validating data during the import, so this feature will get implemented in a future version.

Originally Posted by aaron_w
I can't think of why I'd want to at present, but can you define functions and/or import your own functions?
Not yet, but the syntax will be available to define your own functions.

You can import functions that you write in PHP. It's quite complicated though, and I need to document it along with everything else.

The long-term idea is that developers will be able to write useful PHP functions that you can import into your computations.

Of course, at a certain point, it may be a case of "why don't you just use PHP and do these things yourself?"
Absolutely. And that's the marketing challenge with the Computation Module.

The answer is really this: the code you write for the Computation Module is completely safe. It will never be a security vulnerability (subject to the imported functions, of course; which is another challenge.)

In effect, the computations live in their little sandbox. Also, writing PHP or any other general purpose language is difficult. You need to learn a lot to get started and non-programmers can get into all sorts of mess. Even "real programmers" easily create security vulnerabilities in general languages.

The Computation Module, on the other hand, aims to be simple - if you can understand basic types, and if statements, and write a simple equation, you're fit to go.

But I have been quite impressed by this little form handling framework and it does take some of the regular things you do with forms (error handling, input validation, confirmations, multi-pages, etc) much easier.
Thanks, it's definitely got potential.

BTW, it was originally written to enable generic access to the GeoIP module.

7. Active Member
Join Date
Apr 2008
Posts
23

## Re: Help with Computation

OK, another question here.

Can I do complex conditionals? IE, is there some way to do AND and OR in the computation?

such as

Code:
if (qty_Item1 < 1) and (qty_Item2 < 1)
{
// something
}
Also, can comparisons be inverted? Can I use != or !< or something?

And one more thing ---

When I use the FMUserError() function to throw an error from the module, it shows the error message to the User. But, it doesn't seem to tell me the error message in the alert email it sends me.

All I seem to get is:

Code:
The following error occurred in FormMail :
fmcompute_usererrors
Error=One or more errors occurred in your form submission
Is that by design or am I possibly doing something wrong here?

Thanks --

Aaron

8. ## Re: Help with Computation

Hi,

[quote=aaron_w;10792]
Can I do complex conditionals? IE, is there some way to do AND and OR in the computation?

such as

Code:
if (qty_Item1 < 1) and (qty_Item2 < 1)
{
// something
}
Yes. "and" is "&&" (which for HTML validation you may need to enter as &amp;&amp; - we need to provide a synonym) and "or" is "||"

For example:
Code:
if ((qty_Item1 < 1) && (qty_Item2 < 1))
{
// something
}
or without the redundant parentheses:
Code:
if (qty_Item1 < 1 && qty_Item2 < 1)
{
// something
}
And here's a more complex one:
Code:
if ((item1_name == '' || qty_Item1 < 1) && (item2_name == '' || qty_Item2 < 1))
{
FMUserError('please select at least one item');
}

Also, can comparisons be inverted? Can I use != or !< or something?
Yes, here are the supported operators:
Code:
== equals
!= not equals
> greater than
>= greater than or equal
< less than
<= less than or equal
Note that the opposite of < is >=, the opposite of > is <=.

When I use the FMUserError() function to throw an error from the module, it shows the error message to the User. But, it doesn't seem to tell me the error message in the alert email it sends me.

All I seem to get is:

Code:
The following error occurred in FormMail :
fmcompute_usererrors
Error=One or more errors occurred in your form submission
Is that by design or am I possibly doing something wrong here?
It's implemented to only show the "headline" error in the alert, not all the details.

That could certainly be changed in a future version.

9. Active Member
Join Date
Apr 2008
Posts
23

## Re: Help with Computation

Alright,

Thanks again for your help and further documentation

Note that the opposite of < is >=, the opposite of > is <=.
Yes, thanks for clarifying my thinking there

Originally Posted by russellr

It's implemented to only show the "headline" error in the alert, not all the details.

That could certainly be changed in a future version.
I guess it would be useful to know which errors were triggered.

I find that since I have the computation module, it's easier to write some of the more complex validations there rather than in a "conditions" statement.

Aaron

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts