Collecting form input

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Collecting form input

Sebastian Nozzi-2
Hello all,

when trying to collect the values from a form, I run into a problem.

Let's suppose I write a form to add a new user (user name), together
with an user-type.

I was trying to do something like this:

| newName newType |
html
       form: [html textInput value: 'type name here';
                       callback: [:name | newName := name].
               html select list: userTypes;
                       callback: [:userType | newType := userType];
                       labels: [:userType | userType asString].
               html submitButton
                       callback: [self addUserNamed: newName withType: newType];
                        value: 'Add New User']

But it didn't work. Inside of the callback-block of the submit-button,
both variables were nil.
I searched a little in Internet and the cause seems that when the
block is created, it binds to the value of both variables, which at
that time WERE nil.

Is this correct? I think I read that the new Squeak versions will
correct this "problem"? (something like a new block-compiler...)

Anyway, I reverted to following solution:
(which works)

| params |

params := Dictionary new.
html
       form: [html textInput value: 'type name here';
                       callback: [:name | params at: #name put: name].
               html select list: userTypes;
                       callback: [:userType | params at: #type put: userType];
                       labels: [:userType | userType asString].
               html submitButton
                       callback: [self addUserNamed: (params at:#name)
withType: (params at:#type)];
                        value: 'Add New User']

Did I do the right thing? Or is there a better way?

Thanks!

Sebastian
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Collecting form input

Philippe Marschall
2008/10/4, Sebastian Nozzi <[hidden email]>:

> Hello all,
>
> when trying to collect the values from a form, I run into a problem.
>
> Let's suppose I write a form to add a new user (user name), together
> with an user-type.
>
> I was trying to do something like this:
>
> | newName newType |
> html
>        form: [html textInput value: 'type name here';
>                        callback: [:name | newName := name].
>                html select list: userTypes;
>                        callback: [:userType | newType := userType];
>                        labels: [:userType | userType asString].
>                html submitButton
>                        callback: [self addUserNamed: newName withType:
> newType];
>                         value: 'Add New User']
>
> But it didn't work. Inside of the callback-block of the submit-button,
> both variables were nil.
> I searched a little in Internet and the cause seems that when the
> block is created, it binds to the value of both variables, which at
> that time WERE nil.
>
> Is this correct? I think I read that the new Squeak versions will
> correct this "problem"? (something like a new block-compiler...)
>
> Anyway, I reverted to following solution:
> (which works)
>
> | params |
>
> params := Dictionary new.
> html
>        form: [html textInput value: 'type name here';
>                        callback: [:name | params at: #name put: name].
>                html select list: userTypes;
>                        callback: [:userType | params at: #type put:
> userType];
>                        labels: [:userType | userType asString].
>                html submitButton
>                        callback: [self addUserNamed: (params at:#name)
> withType: (params at:#type)];
>                         value: 'Add New User']
>
> Did I do the right thing? Or is there a better way?

You could also use ValueHolders but that gets quite ugly. I'd use
instance variables and split it up.

Cheers
Philippe
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Collecting form input

Sebastian Nozzi-2
Hi Phillipe,

thanks for your reply.

> You could also use ValueHolders but that gets quite ugly. I'd use
> instance variables and split it up.

I'll look into ValueHolders (never heard of them; but I'm a newbie... so...)

What do you mean by the latter? Because it was with instance variables
that I first tried, but as I said it didn't work:

| newName newSalutation |
html
       form: [html textInput value: 'type name here';
                        callback: [:name | newName := name].
                html select list: salutations;
                        callback: [:userSalutation| newSalutation :=
userSalutation ];
                        labels: [:userSalutation | userSalutation asString].
                html submitButton
                        callback: [self addUserNamed: newName
withSalutation: newSalutation];
                         value: 'Add New User']

When the callback in the submitButton was called, "newName" and
"newSalutation" were nil.

What I was trying to do is to avoid creating a temporary user
instance, and only create it when the user submits the data (the user
could instead opt to go back).

But I think I might be overcomplicating things and I might just create
the temporary instance, since I'm creating a Dictionary anyway...

Greetings,

Sebastian
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Collecting form input

Ryan Simmons-3
Those instance variables, they are temporary variables.
Instance variables are declared in the class declaration something like.

WAComponent subclass: #addUserForm
        instanceVariableNames: 'newName newSalutation'
        classVariableNames: ''
        poolDictionaries: ''
        category: 'MyProject'

-------------
renderContentOn: html
html
     form: [
        html textInput value: 'type name here';
                 callback: [:name | newName := name].
        html select list: salutations;
                  callback: [:userSalutation| newSalutation := userSalutation ];
                  labels: [:userSalutation | userSalutation asString].
        html submitButton
                   callback: [self addUserNamed: newName withSalutation: newSalutation];
                   value: 'Add New User']

Should work fine.
You might be interested in looking at using something like Ramon Leon's
SSForms available in his image that you can download from his blog (http://onsmalltalk.com/)
or Magritte to help you build forms complete with validation.


On Wed, 2008-10-08 at 10:28 +0200, Sebastian Nozzi wrote:

> Hi Phillipe,
>
> thanks for your reply.
>
> > You could also use ValueHolders but that gets quite ugly. I'd use
> > instance variables and split it up.
>
> I'll look into ValueHolders (never heard of them; but I'm a newbie... so...)
>
> What do you mean by the latter? Because it was with instance variables
> that I first tried, but as I said it didn't work:
>
> | newName newSalutation |
> html
>        form: [html textInput value: 'type name here';
>                         callback: [:name | newName := name].
>                 html select list: salutations;
>                         callback: [:userSalutation| newSalutation :=
> userSalutation ];
>                         labels: [:userSalutation | userSalutation asString].
>                 html submitButton
>                         callback: [self addUserNamed: newName
> withSalutation: newSalutation];
>                          value: 'Add New User']
>
> When the callback in the submitButton was called, "newName" and
> "newSalutation" were nil.
>
> What I was trying to do is to avoid creating a temporary user
> instance, and only create it when the user submits the data (the user
> could instead opt to go back).
>
> But I think I might be overcomplicating things and I might just create
> the temporary instance, since I'm creating a Dictionary anyway...
>
> Greetings,
>
> Sebastian
> _______________________________________________
> seaside mailing list
> [hidden email]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Collecting form input

Ryan Simmons-3
Oops that first line should have been:
Those are not instance variables, they are temporary variables.


_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Collecting form input

Sebastian Nozzi-2
In reply to this post by Ryan Simmons-3
Ups, you're right. I misread (was thinking of temporary all the time).

Instance variables... well, that's an idea. Thanks!


2008/10/8 ryan <[hidden email]>:

> Those instance variables, they are temporary variables.
> Instance variables are declared in the class declaration something like.
>
> WAComponent subclass: #addUserForm
>        instanceVariableNames: 'newName newSalutation'
>        classVariableNames: ''
>        poolDictionaries: ''
>        category: 'MyProject'
>
> -------------
> renderContentOn: html
> html
>     form: [
>        html textInput value: 'type name here';
>                 callback: [:name | newName := name].
>        html select list: salutations;
>                  callback: [:userSalutation| newSalutation := userSalutation ];
>                  labels: [:userSalutation | userSalutation asString].
>        html submitButton
>                   callback: [self addUserNamed: newName withSalutation: newSalutation];
>                   value: 'Add New User']
>
> Should work fine.
> You might be interested in looking at using something like Ramon Leon's
> SSForms available in his image that you can download from his blog (http://onsmalltalk.com/)
> or Magritte to help you build forms complete with validation.
>
>
> On Wed, 2008-10-08 at 10:28 +0200, Sebastian Nozzi wrote:
>> Hi Phillipe,
>>
>> thanks for your reply.
>>
>> > You could also use ValueHolders but that gets quite ugly. I'd use
>> > instance variables and split it up.
>>
>> I'll look into ValueHolders (never heard of them; but I'm a newbie... so...)
>>
>> What do you mean by the latter? Because it was with instance variables
>> that I first tried, but as I said it didn't work:
>>
>> | newName newSalutation |
>> html
>>        form: [html textInput value: 'type name here';
>>                         callback: [:name | newName := name].
>>                 html select list: salutations;
>>                         callback: [:userSalutation| newSalutation :=
>> userSalutation ];
>>                         labels: [:userSalutation | userSalutation asString].
>>                 html submitButton
>>                         callback: [self addUserNamed: newName
>> withSalutation: newSalutation];
>>                          value: 'Add New User']
>>
>> When the callback in the submitButton was called, "newName" and
>> "newSalutation" were nil.
>>
>> What I was trying to do is to avoid creating a temporary user
>> instance, and only create it when the user submits the data (the user
>> could instead opt to go back).
>>
>> But I think I might be overcomplicating things and I might just create
>> the temporary instance, since I'm creating a Dictionary anyway...
>>
>> Greetings,
>>
>> Sebastian
>> _______________________________________________
>> seaside mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>
>
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Collecting form input

Philippe Marschall
In reply to this post by Ryan Simmons-3
2008/10/8 ryan <[hidden email]>:

> Those instance variables, they are temporary variables.
> Instance variables are declared in the class declaration something like.
>
> WAComponent subclass: #addUserForm
>        instanceVariableNames: 'newName newSalutation'
>        classVariableNames: ''
>        poolDictionaries: ''
>        category: 'MyProject'
>
> -------------
> renderContentOn: html
> html
>     form: [
>        html textInput value: 'type name here';
>                 callback: [:name | newName := name].
>        html select list: salutations;
>                  callback: [:userSalutation| newSalutation := userSalutation ];
>                  labels: [:userSalutation | userSalutation asString].
>        html submitButton
>                   callback: [self addUserNamed: newName withSalutation: newSalutation];
>                   value: 'Add New User']

Since they are all instance variables you probably also want change
#addUserNamed:withSalutation: to #addUser and access them there
instead of the callback block.

Cheers
Philippe
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside