4 =head2 new Webmin::Form(cgi, [method])
5 Creates a new form, which submits to the given CGI
9 if (defined(&Webmin::Theme::Form::new)) {
10 return new Webmin::Theme::Form(@_[1..$#_]);
12 my ($self, $program, $method) = @_;
13 $self = { 'method' => 'get',
14 'name' => "form".++$form_count };
16 $self->set_program($program);
17 $self->set_method($method) if ($method);
22 Returns the HTML that makes up this form
28 if ($self->get_align()) {
29 $rv .= "<div align=".$self->get_align().">\n";
31 $rv .= $self->form_start();
32 if ($self->get_heading()) {
33 if (defined(&ui_subheading)) {
34 $rv .= &ui_subheading($self->get_heading());
37 $rv .= "<h3>".$self->get_heading()."</h3>\n";
42 foreach my $h (@{$self->{'hiddens'}}) {
43 $rv .= &ui_hidden($h->[0], $h->[1])."\n";
45 foreach my $s (@{$self->{'sections'}}) {
49 # Check if we have any inputs that need disabling
50 my @dis = $self->list_disable_inputs();
52 # Yes .. generate a function for them
54 $rv .= "function ui_disable_".$self->{'name'}."(form) {\n";
55 foreach my $i (@dis) {
56 foreach my $n ($i->get_input_names()) {
57 $rv .= " form.".$n.".disabled = (".
58 $i->get_disable_code().");\n";
65 # Add the buttons at the end of the form
67 foreach my $b (@{$self->{'buttons'}}) {
70 my $ihtml = join(" ", map { $_->html() } @$b);
71 push(@buttonargs, $ihtml);
75 push(@buttonargs, "");
78 $rv .= &ui_form_end(\@buttonargs);
80 if ($self->get_align()) {
84 # Call the Javascript disable function
87 $rv .= "ui_disable_".$self->{'name'}."(window.document.forms[\"$self->{'name'}\"]);\n";
97 return "<form action='$self->{'program'}' ".
98 ($self->{'method'} eq "post" ? "method=post" :
99 $self->{'method'} eq "form-data" ?
100 "method=post enctype=multipart/form-data" :
101 "method=get")." name=$self->{'name'}>\n";
104 =head2 add_section(section)
105 Adds a Webmin::Section object to this form
109 my ($self, $section) = @_;
110 push(@{$self->{'sections'}}, $section);
111 $section->set_form($self);
114 =head2 get_section(idx)
118 my ($self, $idx) = @_;
119 return $self->{'sections'}->[$idx];
122 =head2 add_button(button, [beside, ...])
123 Adds a Webmin::Submit object to this form, for display at the bottom
127 my ($self, $button, @beside) = @_;
128 push(@{$self->{'buttons'}}, [ $button, @beside ]);
131 =head2 add_button_spacer()
132 Adds a gap between buttons, for grouping
134 sub add_button_spacer
136 my ($self, $spacer) = @_;
137 push(@{$self->{'buttons'}}, $spacer);
140 =head2 add_hidden(name, value)
141 Adds some hidden input to this form, for passing to the CGI
145 my ($self, $name, $value) = @_;
146 push(@{$self->{'hiddens'}}, [ $name, $value ]);
150 Validates all form inputs, based on the current CGI input hash. Returns a list
151 of errors, each of which is field name and error message.
157 foreach my $s (@{$self->{'sections'}}) {
158 push(@errs, $s->validate($self->{'in'}));
163 =head2 validate_redirect(page, [&extra-errors])
164 Validates the form, and if any errors are found re-directs to the given page
165 with the errors, so that they can be displayed.
167 sub validate_redirect
169 my ($self, $page, $extras) = @_;
170 if ($self->{'in'}->{'ui_redirecting'}) {
171 # If this page is displayed as part of a redirect, no need to validate!
174 my @errs = $self->validate();
175 push(@errs, @$extras);
177 my (@errlist, @vallist);
178 foreach my $e (@errs) {
179 push(@errlist, &urlize("ui_error_".$e->[0])."=".
182 foreach my $i ($self->list_inputs()) {
183 my $v = $i->get_value();
184 my @vals = ref($v) ? @$v : ( $v );
185 @vals = ( undef ) if (!@vals);
188 &urlize("ui_value_".$i->get_name())."=".
192 foreach my $h (@{$self->{'hiddens'}}) {
194 &urlize($h->[0])."=".&urlize($h->[1]));
196 if ($page =~ /\?/) { $page .= "&"; }
197 else { $page .= "?"; }
198 &redirect($page.join("&", "ui_redirecting=1", @errlist, @vallist));
203 =head2 validate_error(whatfailed)
204 Validates the form, and if any errors are found displays an error page.
208 my ($self, $whatfailed) = @_;
209 my @errs = $self->validate();
210 &error_setup($whatfailed);
212 &error($errs[0]->[2] ? "$errs[0]->[2] : $errs[0]->[1]"
216 my $msg = $text{'ui_errors'}."<br>";
217 foreach my $e (@errs) {
218 $msg .= $e->[2] ? "$e->[2] : $e->[1]<br>\n"
225 =head2 field_errors(name)
226 Returns a list of error messages associated with the field of some name, from
227 the input passed to set_input
231 my ($self, $name) = @_;
233 my $in = $self->{'in'};
234 foreach my $i (keys %$in) {
235 if ($i eq "ui_error_".$name) {
236 push(@errs, split(/\0/, $in->{$i}));
242 =head2 set_input(&input)
243 Passes the form input hash to this form object, for use by the validate
244 functions and for displaying errors next to fields.
248 my ($self, $in) = @_;
252 =head2 get_value(input-name)
253 Returns the value of the input with the given name.
257 my ($self, $name) = @_;
258 foreach my $s (@{$self->{'sections'}}) {
259 my $rv = $s->get_value($name);
260 return $rv if (defined($rv));
262 return $self->{'in'}->{$name};
265 =head2 get_input(name)
266 Returns the input with the given name
270 my ($self, $name) = @_;
271 foreach my $i ($self->list_inputs()) {
272 return $i if ($i->get_name() eq $name);
279 my ($self, $program) = @_;
280 $self->{'program'} = $program;
285 my ($self, $method) = @_;
286 $self->{'method'} = $method;
290 Returns all inputs in all form sections
296 foreach my $s (@{$self->{'sections'}}) {
297 push(@rv, $s->list_inputs());
302 =head2 list_disable_inputs()
303 Returns a list of inputs that have disable functions
305 sub list_disable_inputs
309 foreach my $i ($self->list_inputs()) {
310 push(@dis, $i) if ($i->get_disable_code());
315 =head2 set_page(Webmin::Page)
316 Called when this form is added to a page
320 my ($self, $page) = @_;
321 $self->{'page'} = $page;
324 =head2 get_changefunc(&input)
325 Called by some input, to return the Javascript that should be called when this
326 input changes it's value.
330 my ($self, $input) = @_;
331 my @dis = $self->list_disable_inputs();
333 return "ui_disable_".$self->{'name'}."(form)";
338 =head2 set_heading(text)
339 Sets the heading to be displayed above the form
343 my ($self, $heading) = @_;
344 $self->{'heading'} = $heading;
350 return $self->{'heading'};
354 Returns the index of this form on the page
360 foreach my $f (@{$self->{'page'}->{'contents'}}) {
364 elsif (ref($f) =~ /Form/) {
371 =head2 add_onload(code)
372 Adds some Javascript code for inclusion in the onLoad tag
376 my ($self, $code) = @_;
377 push(@{$self->{'onloads'}}, $code);
380 =head2 add_script(code)
381 Adds some Javascript code for putting in the <head> section
385 my ($self, $script) = @_;
386 push(@{$self->{'scripts'}}, $script);
389 =head2 set_align(align)
390 Sets the alignment on the page (left, center, right)
394 my ($self, $align) = @_;
395 $self->{'align'} = $align;
401 return $self->{'align'};