@@ -1907,6 +1907,24 @@ C<L<< form_name()|/"$mech->form_name( $name [, \%args ] )" >>> or
1907
1907
C<L<< form_number()|/"$mech-> form_number($number)" >>>
1908
1908
method or defaulting to the first form on the page).
1909
1909
1910
+ If the field is of type "file", its value should be an arrayref. Example:
1911
+
1912
+ $mech->field( $file_input, ['/tmp/file.txt'] );
1913
+
1914
+ Value examples for "file" inputs, followed by explanation of what each
1915
+ index mean:
1916
+
1917
+ ['/tmp/file.txt']
1918
+ ['/tmp/file.txt', 'filename.txt']
1919
+ ['/tmp/file.txt', 'filename.txt', @headers]
1920
+ ['/tmp/file.txt', 'filename.txt', Content => 'some content']
1921
+ [undef, 'filename.txt', Content => 'content here']
1922
+
1923
+ Index 0 is the filepath that will be read from disk. Index 1 is the
1924
+ filename which will be used in the HTTP request body; if not given,
1925
+ filepath (index 0) is used instead. If "Content => 'content here'" is
1926
+ informed as shown, then filepath will be ignored.
1927
+
1910
1928
The optional C<$number > parameter is used to distinguish between two fields
1911
1929
with the same name. The fields are numbered from 1.
1912
1930
@@ -1922,7 +1940,16 @@ sub field {
1922
1940
}
1923
1941
else {
1924
1942
if ( ref ($value ) eq ' ARRAY' ) {
1925
- $form -> param($name , $value );
1943
+ my $input = $form -> find_input($name );
1944
+
1945
+ if ( $input -> type eq ' file' ) {
1946
+ $input -> file( shift @$value );
1947
+ $input -> filename( shift @$value );
1948
+ $input -> headers( @$value );
1949
+ }
1950
+ else {
1951
+ $form -> param($name , $value );
1952
+ }
1926
1953
}
1927
1954
else {
1928
1955
$form -> value($name => $value );
@@ -2041,6 +2068,19 @@ which has the field value and its number as the 2 elements.
2041
2068
# set the second $name field to 'foo'
2042
2069
$mech->set_fields( $name => [ 'foo', 2 ] );
2043
2070
2071
+ The value of a field of type "file" should be an arrayref as described
2072
+ in C<L<< field()|$mech-> field( $name, $value, $number ) >>>. Examples:
2073
+
2074
+ $mech->set_fields( $file_field => ['/tmp/file.txt'] );
2075
+ $mech->set_fields( $file_field => ['/tmp/file.txt', 'filename.txt'] );
2076
+
2077
+ The value for a "file" input can also be an arrayref containing an
2078
+ arrayref and a number, as documented in
2079
+ C<L<< submit_form()|$mech-> submit_form( ... ) >>>.
2080
+ The number will be used to find the field in the form. Example:
2081
+
2082
+ $mech->set_fields( $file_field => [['/tmp/file.txt'], 1] );
2083
+
2044
2084
The fields are numbered from 1.
2045
2085
2046
2086
For fields that have a predefined set of values, you may also provide a
@@ -2065,10 +2105,20 @@ sub set_fields {
2065
2105
FIELD:
2066
2106
for my $field ( keys %fields ) {
2067
2107
my $value = $fields {$field };
2108
+ my $number = 1;
2068
2109
2069
2110
if ( ref $value eq ' ARRAY' ) {
2070
- $form -> find_input( $field , undef ,
2071
- $value -> [1])-> value($value -> [0] );
2111
+ my $input = $form -> find_input($field );
2112
+
2113
+ # Honor &submit_form's documentation, that says that a
2114
+ # "file" input's value can be in the form of
2115
+ # "[[$filepath, $filename], 1]".
2116
+ if (
2117
+ $input -> type ne ' file'
2118
+ || ( $input -> type eq ' file' && ref ( $value -> [0] ) eq ' ARRAY' )
2119
+ ) {
2120
+ ( $value , $number ) = ( $value -> [0], $value -> [1] );
2121
+ }
2072
2122
}
2073
2123
else {
2074
2124
if ( ref $value eq ' SCALAR' ) {
@@ -2086,9 +2136,8 @@ sub set_fields {
2086
2136
}
2087
2137
$value = $possible_values [ $$value ];
2088
2138
}
2089
-
2090
- $form -> value($field => $value );
2091
2139
}
2140
+ $self -> field($field , $value , $number );
2092
2141
}
2093
2142
}
2094
2143
0 commit comments