Skip to content

Commit

Permalink
Item11966: implement *Default* column in form definition topics feature
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk@15709 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Oct 25, 2012
1 parent 2ca758a commit 82fccbe
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 26 deletions.
107 changes: 93 additions & 14 deletions UnitTestContrib/test/unit/InitFormTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,20 @@ my $testtmpl1 = <<'HERE';
HERE

my $testform1 = <<'HERE';
| *Name* | *Type* | *Size* | *Values* | *Tooltip messages* | *Mandatory* |
| Issue Name | text | 73 | My first defect | Illustrative name of issue | M |
| Issue Description | textarea | 55x5 | Simple description of problem | Short description of issue | |
| Issue Type | select | 1 | Defect, Enhancement, Other | | |
| History1 | label | 1 | %ATTACHURL% | | |
| History2 | text | 20 | %ATTACHURL% | | |
| History3 | label | 1 | $percntATTACHURL% | | |
| History4 | text | 20 | $percntATTACHURL% | | |
| *Name* | *Type* | *Size* | *Values* | *Tooltip messages* | *Mandatory* | *Default* |
| Issue Name | text | 73 | My first defect over-ridden | Illustrative name of issue | M | My first defect |
| Issue Description | textarea | 55x5 | Simple description of problem | Short description of issue | |Simple description of problem |
| Issue Type | select | 1 | Defect, Enhancement, Other | | | |
| History1 | label | 1 | %ATTACHURL% | | | %ATTACHURL%|
| History2 | text | 20 | %ATTACHURL% | | | %ATTACHURL%|
| History3 | label | 1 | $percntATTACHURL% | | |e |
| History4 | text | 2 | this will not be used as its over-ridden by the next line | | | |
| History4 | text | 20 | $percntATTACHURL% | | |$percntATTACHURL% |
| NewWithDefault | text | 20 | $percntATTACHURL% | the default col over-rides value | | is it a plane? |
| [[NewWithDefaultOnly][default only]] | text | 20 | | | | is it another plane? |
| Default To Hidden | select | 1 | Defect, Enhancement, Hidden, Other | | | Hidden |
| Default To Enhancement | radio | 1 | Defect, Enhancement, Hidden, Other | | | Enhancement |
HERE

Expand Down Expand Up @@ -400,8 +406,13 @@ sub test_form {
my $text = setup_formtests( $this, $testweb, $testtopic1,
"formtemplate=\"$testweb.$testform\"" );

my $value = 'My first defect';
$value = 'My first defect over-ridden'
if ( $this->check_dependency('Foswiki,<,1.2') );
$this->assert_html_matches(
'<input type="text" name="IssueName" value="My first defect" size="73" class="foswikiInputField foswikiMandatory" />',
'<input type="text" name="IssueName" value="'
. $value
. '" size="73" class="foswikiInputField foswikiMandatory" />',
get_formfield( 1, $text )
);
$this->assert_html_matches(
Expand All @@ -421,12 +432,74 @@ Simple description of problem</textarea>', get_formfield( 2, $text )
. '" size="20" class="foswikiInputField" />',
get_formfield( 5, $text )
);

$value = 'e';
$value = '%ATTACHURL%' if ( $this->check_dependency('Foswiki,<,1.2') );
$this->assert_html_matches(
'<input type="hidden" name="History3" value="%ATTACHURL%" />',
get_formfield( 6, $text ) );
'<input type="hidden" name="History3" value="' . $value . '" />',

#.'<div class="foswikiFormLabel">http://quiet/~sven/core/pub/TemporaryTestWeb/InitTestTopic1</div>',
get_formfield( 6, $text )
);

if ( $this->check_dependency('Foswiki,<,1.2') ) {

#TODO: SMELL: in 1.1 (need to test 1.0), duplicate fields in the form will result in duplicate html,
$this->assert_html_matches(
'<input type="text" name="History4" value="this will not be used as its over-ridden by the next line" size="2" class="foswikiInputField" />',
get_formfield( 8, $text )
);
}
else {
# $this->assert_html_not_matches(
#'<input type="text" name="History4" value="this will not be used as its over-ridden by the next line" size="2" class="foswikiInputField" />',
# get_formfield( 8, $text )
# );
}
$this->assert_html_matches(
'<input type="text" name="History4" value="%ATTACHURL%" size="20" class="foswikiInputField" />',
get_formfield( 7, $text )
get_formfield( 9, $text )
);

#| NewWithDefault | text | 20 | $percntATTACHURL% | the default col over-rides value | | is it a plane? |
$value = 'is it a plane?';
$value = '%ATTACHURL%' if ( $this->check_dependency('Foswiki,<,1.2') );

$this->assert_html_matches(
'<input type="text" name="NewWithDefault" value="'
. $value
. '" size="20" class="foswikiInputField" />',
get_formfield( 10, $text )
);

#| [[NewWithDefaultOnly][default only]] | text | 20 | | | | is it another plane? |
$value = ' value="is it another plane?"';
$value = '' if ( $this->check_dependency('Foswiki,<,1.2') );
$this->assert_html_matches(
'<input type="text" name="defaultonly"'
. $value
. ' size="20" class="foswikiInputField" />',
get_formfield( 11, $text )
);

#| Default To Hidden | select | 1 | Defect, Enhancement, Hidden, Other | | | Hidden |
$value = ' selected="selected"';
$value = '' if ( $this->check_dependency('Foswiki,<,1.2') );
$this->assert_html_matches(
'<select name="DefaultToHidden" class="foswikiSelect" size="1"><option class="foswikiOption">Defect</option><option class="foswikiOption">Enhancement</option><option class="foswikiOption"'
. $value
. '>Hidden</option><option class="foswikiOption">Other</option></select>',
get_formfield( 12, $text )
);

#| Default To Enhancement | radio | 1 | Defect, Enhancement, Hidden, Other | | | Enhancement |
$value = 'checked="checked" ';
$value = '' if ( $this->check_dependency('Foswiki,<,1.2') );
$this->assert_html_matches(
'<input type="radio" name="DefaultToEnhancement" value="Defect" title="Defect" class="foswikiRadioButton"/>Defect</label></td></tr><tr><td><label><input type="radio" name="DefaultToEnhancement" value="Enhancement" '
. $value
. ' title="Enhancement" class="foswikiRadioButton"/>Enhancement</label></td></tr><tr><td><label><input type="radio" name="DefaultToEnhancement" value="Hidden" title="Hidden" class="foswikiRadioButton"/>Hidden</label></td></tr><tr><td><label><input type="radio" name="DefaultToEnhancement" value="Other" title="Other" class="foswikiRadioButton"/>Other</label>',
get_formfield( 13, $text )
);

return;
Expand Down Expand Up @@ -462,9 +535,15 @@ Simple description of problem</textarea>', get_formfield( 2, $text )
. '" size="20" class="foswikiInputField" />',
get_formfield( 5, $text )
);
my $value = 'e';
$value = '%ATTACHURL%' if ( $this->check_dependency('Foswiki,<,1.2') );
$this->assert_html_matches(
'<input type="hidden" name="History3" value="%ATTACHURL%" />',
get_formfield( 6, $text ) );
'<input type="hidden" name="History3" value="' . $value . '" />',

#.'<div class="foswikiFormLabel">http://quiet/~sven/core/pub/TemporaryTestWeb/InitTestTopic1</div>',
get_formfield( 6, $text )
);

$this->assert_html_matches(
'<input type="text" name="History4" value="%ATTACHURL%" size="20" class="foswikiInputField" />',
get_formfield( 7, $text )
Expand Down
22 changes: 13 additions & 9 deletions core/data/System/DataForms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ Topics can store data as name/value pairs, or form fields. The attributes of eac
---+++ The data form topic
The data definition is defined in a TML table and looks like this:
<verbatim class="tml">
| *Name* | *Type* | *Size* | *Values* | *Tooltip message* | *Attributes* |
| TopicTitle | text | 100 | | | H |
| Version | select | 1 | ,Version in SVN,Foswiki 1.1.4,Foswiki 1.1.3,Foswiki 1.1.2,Foswiki 1.1.1 | | |
| Status | select | 1 | Asked,More info required,Answered | | M |
| Related Topics | textboxlist | | | |
| *Name* | *Type* | *Size* | *Values* | *Tooltip message* | *Attributes* | *Default* |
| TopicTitle | text | 100 | | | H | |
| Version | select | 1 | Version in SVN,Foswiki 1.1.4,Foswiki 1.1.3,Foswiki 1.1.2,Foswiki 1.1.1 | | | |
| Status | select | 1 | Asked,More info required,Answered | | M | Asked |
| Related Topics | textboxlist | | | | |
</verbatim>
In the next section we'll go into the details of the table contents.

Expand Down Expand Up @@ -110,6 +110,7 @@ Each form field is defined by these attributes:
| Values | For checkboxes, radio buttons and dropdown lists: predefined input to select from. More advanced: this can be a dynamically generated list of values. |
| Tooltip message | (hardly used or useful anymore) A message that will be displayed when the cursor is hovered over the field in edit view. |
| Attributes | Whether the field is mandatory or hidden in view mode. |
| Default | %NEW% if there is a =default= column, its value (or lack of value) is used as the default for new fields. This allows you to default the value of =select=, =radio= and =checkbox= to something other than the first =values= element |

As said, only =Name=, =Type= and =Size= are required.

Expand Down Expand Up @@ -145,7 +146,7 @@ The control appearance is also specified by size and (initial) value. More on th
| A single-line text box and a calendar icon button next to it; clicking on the button will bring up a calendar from which the user can select a date. \
The date can also be typed into the text box. \
| The text box width in characters. \
| The initial text. \
| The initial text (unless =default= column exists). \
| |
| =label= \
| Read-only label text. \
Expand All @@ -166,12 +167,12 @@ If you specify a range, the box will never be smaller than 3 items, never larger
| =text= \
| A one-line text field. \
| The text box width in number of characters. \
| The initial (default) content when a new topic is created with this form definition. \
| The initial (default) content when a new topic is created with this form definition (unless =default= column exists). \
| |
| =textarea= \
| A multi-line text box. \
| Size in columns x rows, e.g. =80x6=; default size is 40x5. \
| The initial text. \
| The initial text (unless =default= column exists). \
| |

<div class='foswikiHelp'>
Expand Down Expand Up @@ -226,14 +227,17 @@ The input size of the form field inputs on the edit screen. The size acts a bit
---++++ Values
For checkboxes, radio buttons and dropdown lists: predefined input to select from. More advanced: this can be a dynamically generated list of values.

* The field value will be used to initialize a field when a form is created, unless specific values are given by the topic template or query parameters. The first item in the list for a select or radio type is the default item. For =label=, =text=, and =textarea= fields the value may also contain commas. =checkbox= fields cannot be initialized through the form definition.
* The field value will be used to initialize a field when a form is created (unless =default= column exists), unless specific values are given by the topic template or query parameters. The first item in the list for a select or radio type is the default item. For =label=, =text=, and =textarea= fields the value may also contain commas. =checkbox= fields cannot be initialized through the form definition.
* Leading and trailing spaces do not matter.
* Field values can also be generated through a %SYSTEMWEB%.FormattedSearch, which must yield a suitable table as the result.
* Macros in the initial values of a form definition get expanded when the form definition is loaded.
* If you want to use a =|= character in the initial values field, you have to precede it with a backslash, thus: =\|=.
* You can use =&lt;nop>= to prevent macros from being expanded.
* The [[%SYSTEMWEB%.FormatTokens][Format tokens]] can be used to prevent expansion of other characters.

---++++ Default
If this column exists in the form definition, then it will be used to determine the default value for a field. It over-rides all defaulting from the =Values= column.

<div class='foswikiHelp'>
---+++++ How to retrieve values from other topics
If you have rows defined like this:
Expand Down
14 changes: 14 additions & 0 deletions core/lib/Foswiki/Form.pm
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ sub _parseFormDefinition {
$vals =~ s/\s+$//g;
$field{value} = $vals;

#repeat same again with {default}
if ( exists( $field{default} ) ) {
my $vals = $field{default} || '';
if ( $vals =~ /%/ ) {
$vals = $this->expandMacros($vals);
}
$vals =~ s/<\/?(!|nop|noautolink)\/?>//g;

# Trim again in case macro expansion has added spaces
$vals =~ s/^\s+//g;
$vals =~ s/\s+$//g;
$field{default} = $vals;
}

$field{attributes} =~ s/\s*//g if ( exists( $field{attributes} ) );

$field{title} = $field{name};
Expand Down
4 changes: 2 additions & 2 deletions core/lib/Foswiki/Form/FieldDefinition.pm
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ the form definition. In that case this method should return =undef=.
sub getDefaultValue {
my $this = shift;

#my $value = (exists($this->{default})?$this->{default}:$this->{value});
my $value = $this->{value};
my $value =
( exists( $this->{default} ) ? $this->{default} : $this->{value} );
$value = '' unless defined $value; # allow 0 values

return $value;
Expand Down
3 changes: 2 additions & 1 deletion core/lib/Foswiki/Form/Select.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ on a value to the browser.
=cut

sub getDefaultValue {
return '';
my $this = shift;
return ( exists( $this->{default} ) ? $this->{default} : '' );
}

sub getOptions {
Expand Down

0 comments on commit 82fccbe

Please sign in to comment.