Gossamer Forum
Home : Products : DBMan : Customization :

Multiple selects lost at add record failure

Quote Reply
Multiple selects lost at add record failure
I have an Add Record form with one multiple select field and with several of the other fields (including the multiple select field) set up as Required. If I forget some of the required fields the adding of the record fails, which is all right.

The problem is that the multiple select field has lost the information when the record is returned, and if submit the record with the originally missing field corrected, I get an error due to the lost information in the multiple select field.

The same thing happens with the preview mod that I just added: In the form part of the preview page the multiple select field is blank, the choices that were previously highlighted has gone. In the preview part it looks all right.

So somehow the read back seems to be wrong. What could it be?

Quote Reply
Re: Multiple selects lost at add record failure In reply to
If you html.pl file, close up the gaps in that field.

['Value1','Value1'],
['Value2','Value2'],
['Value3','Value3'],
['Value4','Value4'],


make sure its like that and not

['Value1', 'Value1'],
['Value2', 'Value2'],
['Value3', 'Value3'],
['Value4', 'Value4'],


i hope that helps. I had the same problem and that fixed it for me.



Quote Reply
Re: Multiple selects lost at add record failure In reply to
Thanks Ulic,

But I don't recognise your code at all in html.pl. Do you use the fancy select field mod (or whatever it is called)?

This is what I have got in default.cfg under section Select fields:

'Countries' => 'Albania,Andorra,Austria,Belarus,Belgium,Bosnia_Herzegovina,Bulgaria,Croatia,Czech_Republic,Denmark,Estonia,Finland,France,Germany,Greece,Hungary,Iceland,Ireland,Italy,Latvia,Lichtenstein,Lithuania,Luxemborg,Macedonia,Malta,Moldova,Monaco,Netherlands,Norway,Poland,Portugal,Romania,Russia,San_Marino,Slovakia,Slovenia,Spain,Sweden,Switzerland,Turkey,Turkmenistan,Ukraine,United_Kingdom,Yugoslavia'

I can't see any spaces there.

And this I have in html.pl in sub html_record_form

print &build_select_field("Countries",$rec{'Countries'},"Countries","MULTIPLE",7);

and finally the sub build_select_fields in db.cgi as modified by JPDeni
Code:

################ BUILD MULTIPLE SELECT ########
sub build_select_field {

# --------------------------------------------------------
# Builds a SELECT field based on information found
# a default value, a default name, whether to allow multiple selections,
# and the size of the box.

my ($column, $value, $name, $mult, $size) = @_;
my (@fields, @values, $ouptut);
$name || ($name = $column);
$size || ($size = 1);
if ($mult) { @values = split (/\Q$db_delim\E/,$value); }

else { @values = ($value); }
@fields = split (/\,/, $db_select_fields{$column});
if (! exists ($db_select_fields{$column})) {
$output = "error building select field: no fields specified/unkown field ($column)!";
}
else {
$output = qq|<SELECT NAME="$name" $mult SIZE=$size>|;
foreach $field (@fields) {
if (grep $_ eq $field, @values) {
$output .= "<OPTION SELECTED>$field\n";

}
else {
$output .= "<OPTION>$field";
}
}
$output .= "</SELECT>";
}
return $output;
}
It seems that the problem is with the red-marked lines, or maybe the input @values to the sub is lost?

Anyway it's comforting that someone has had the same problem but got it solved.


[Edited 27 nov: new suspected lines coloured green]
Quote Reply
Re: Multiple selects lost at add record failure In reply to
I did use the fancy field. it works great, just took a little big of tweaking.

Im looking at the red area, and I dont think im gonna be much help, but I dont understand exactly what that is doing. but its checking to see if there are values and are using those. So the process is right, but something doesnt quite work.

Whats is the URL?

Quote Reply
What is /\Q$db_delim\E/ doing? In reply to
Hi Ulic!

Oh, that long line without spaces is a nuisance!

Anyway I think your are right when you say that the red-marked line works.

I suddenly realize that the problem is a bit earlier, and I don't understand how this works (limited Perl knowledge, I confess). What is the portion /\Q$db_delim\E/ in the split doing? Is this where it goes wrong

In the previous post I have green marked the lines that are now in focus.

The URL is
http://tangokites.org/...al/db.cgi?db=teg_o_E

User ID and Password is DBtest.

I am working on an update (adding a few mods) in a test environment so it's safe (I hope!) to play around. The debug is switched on, too.

Quote Reply
Re: What is /\Q$db_delim\E/ doing? In reply to
can you give me a link to a txt version of your cfg file? The settings may be important.

Looking at the debug stuff, it makes it into the database, but then doesnt work.....the preview thing may have something to do with that.

Quote Reply
What does /\Q$db_delim\E/ mean? In reply to
I suddenly remebered something from a thread about a year ago that from a multiple select field the selected values are stored with a ~~ between each entry. That thread also gave a tip on how to substitute the ~~ with a , (comma) before displaying, which I use in html_record_long.

Then in build_select_field the list is split at \Q$db_delim\E, and I think that this is were it fails. I have experimented with removing the substitution in html_record_long, but it still goes wrong.

Can you or anyone else tell me what the \Q and \E around $db_delim means? Can't find it in my Perl book.

Maybe I put the substituition wrong in the first place? This was when I just started with DBman and was new with Perl Wink



Quote Reply
I got it! In reply to
The problem seems to be that before the record has been saved each value is separated by ~~. When a record is retrieved from the database the ~~ is converted by sub split_decode to $db_delim.

So my solution is to modify one line in sub build_select_fields:

Code:

if ($mult) { @values = split (/\Q$db_delim\E/,$value); }
to
Code:

if ($mult) { @values = split (/\~\~/,$value); }
and for every field that is of multiple_select type add a line in sub html_record_form:
Code:

$rec{'Field1'} =~ s/\Q$db_delim\E/\~\~/g;
to prepare the valuelist for build_select_fields
Quote Reply
Re: I got it! In reply to
O grain:

I could be wrong here but when you are using the muliple select mod and calling this sub you stated you were using something like:

print &build_select_field ("FieldName", "$rec{'FieldName'}");

I belive you have to refer to the new sub build_multiple_select_field {

by referring to it as in the following example:

print &build_multiple_select_field ("FieldName", "$rec{'FieldName'}",3);

Using: &build_multiple_select_field
instead of: &build_select_field

This is the sub which should be added to your db.cgi file:

sub build_multiple_select_field {
# --------------------------------------------------------
# Builds a multiple SELECT field based on information found
# in the database definition.
my ($column, $value, $size) = @_;
my (@fields, @values, $ouptut);

$size or ($size = 3);

@values = split (/\Q$db_delim\E/,$value);
@fields = split (/\,/, $db_select_fields{$column});
if (! exists ($db_select_fields{$column})) {
$output = "error building select field: no fields specified/unkown field ($column)!";
}
else {
$output = qq|<SELECT NAME="$column" MULTIPLE SIZE=$size><OPTION>---|;
foreach $field (sort @fields) {
if (grep $_ eq $field, @values) {
$output .= "<OPTION SELECTED>$field\n";
}
else {
$output .= "<OPTION>$field";
}
}
$output .= "</SELECT>";
}
return $output;
}


Hope this helps and that I didn't just confuse the issue futher.

Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/
Quote Reply
Re: "new" sub build_multiple_select_fields In reply to
Hi LoisC,

You might be right, but as I remember it (this was the very first mod I added!) the original build_select_field was modified to handle both single and multiple select, which it does!

And the call was modified with a few extra items :
&build_select_field("Countries",$rec{'Countries'},"Countries","MULTIPLE",7);

The problem was that the build-sub did not handle selected items properly in e.g. preview or add failure (values separated by ~~), only when items were retrieved from the database (values separated by $db_delim). And as far as I can see this is the case also with the build_multiple_select_field that you show.

Maybe you can explain why the delimiter ~~ is converted to $db-delim by sub split_decode? Is this in the original code?

What happens now is that first ~~ is converted to $db_delim in split_decode, then in sub html_long $db_delim is converted to , (comma) and then again back to ~~ before the call of build_select_field

You didn't manage to confuse me this time Wink!



Quote Reply
Re: "new" sub build_multiple_select_fields In reply to
I wish I could explain how things work, but I'm not a programmer and have only learned by following the many examples provided in the support forum.

Have you read all the related thread in the FAQ noted below?
I'm sure you will find answers there as I have referenced numerous threads in the forum. Try looking under "Fields" and then under "Working with Selects".

I find reading all related threads helps due to finding answers where I least expect to find them.

Glad I didn't confuse you this time Smile

Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/