form_example.php 4.75 KB
Newer Older
1 2 3
<?php
#
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35
#
include("defs.php3");
include("form_defs.php");

#
# Only known and logged in users.
#
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();

$optargs = OptionalPageArguments("submit",      PAGEARG_STRING,
				 "formfields",  PAGEARG_ARRAY);

36 37
PAGEHEADER("Silly Forms example");

38 39 40
# The form attributes:
$form = array('#id'	  => 'form1',
	      '#caption'  => 'My Form',
41 42
	      # XXX Only use enctype when doing a file upload. See below.
	      '#enctype'  => 'multipart/form-data',
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	      '#action'   => 'form_example.php');

# A set of form fields.
$fields = array();

# Text field.
$fields['slot1'] = array('#type'        => 'textfield',
			 '#label'       => 'Slot1',
			 '#value'       => 'Hello World',
			 '#size'        => 60,
			 '#maxlength'   => 64,
			 '#description' => 'Alphanumeric, No Blanks'
			 );

# Password
$fields['slot2'] = array('#type'	 => 'password',
			 '#label'        => 'Password',
			 '#required'     => TRUE,
			 '#footnote'     => 'This is a footnote',
			 '#size'         => 8);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#
# File Upload. You must set '#enctype' in the form array above.
#
# Files are special cause PHP puts all the stuff into the $_FILES
# superglobal. So, there is nothing in $formfields for them. For
# doing the validation, easiest to define a local function.
#
function CheckFileUpload($name, &$errors, $attributes, $value)
{
    # Note that $value will not be defined in this function.
    
    if (! isset($_FILES[$name]['name']) || $_FILES[$name]['size'] == 0) {
	$errors[$name] = "Missing required value";
	return;
    }
}
80 81 82
$fields['myfile'] = array('#type'        => 'file',
			  '#label'       => 'Your File',
			  '#size'        => 30,
83 84
			  '#checkslot'   => 'CheckFileUpload',
			  '#description' => 'A File'
85
			  );
86
$fields['MAX_FILE_SIZE'] = array('#type' => 'hidden', '#value' => "1024");
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

# A plain checkbox
$fields['slot3']  = array('#type'        => 'checkbox',
			  '#return_value'=> "Yep",
			  '#label'       => 'Check this Box',
			  );

# A list of checkboxes
$fields['slot4']  = array('#type'        => 'checkboxes',
			  '#label'       => 'Check some of these',
			  '#boxes'       =>
			    array('box1' => array('#return_value' => "Yep",
						  '#label' => 'L1'
						  ),
				  'box2' => array('#return_value' => "Yep",
						  '#label' => 'L2'
						  ),
				  'box3' => array('#return_value' => "Yep",
						  '#label' => 'L3'
						  ),
				  ),
			  );

# A radio checklist.
$fields['slot5']  = array('#type'       => 'radio',
			  '#label'      => 'Check one of these',
			  '#radios'     =>
			   array('Rad1' => array('#label' => 'R1',
						 '#return_value' => "R1v",
						 ),
				 'Rad2' => array('#label' => 'R2',
						 '#return_value' => "R2v",
						 ),
				 'Rad3' => array('#label' => 'R3',
						 '#return_value' => "R3v",
						 ),
				 ),
			  );

# A selection list.
$fields['slot6']  = array('#type'       => 'select',
			  '#label'      => 'A selection of items',
			  '#default_value' => 'B',
			  '#options'    => array('A' => 'Pick A',
						 'B' => 'Pick B',
						 'C' => 'Pick C'),
			  '#description'=> 'Bla Bla Bla'
			  );

# A hidden Field.
$fields['slot7']  = array('#type'  => 'hidden',
			  '#value' => "69",
			  );

# A simple subsection (rendered as an inner table).
$fields['address'] = array('#type'   => 'table',
			   '#label'  => 'Fill in your addresss',
			   '#fields' =>
			    array('part1' => array('#type' => 'textfield',
						   '#label' => 'Street',
						   '#size' => 60,
						   ),
				  'part2' => array('#type' => 'textfield',
						   '#label' => 'Town',
						   '#value' => 'Corvallis',
						   '#size' => 30,
						   ),
				  ),
			   );
# The submit button.				
$fields['submit']  = array('#type'  => 'submit',
			   '#value' => "Submit This Form",
			   );

161 162 163 164 165 166 167 168 169
$errors  = array();

if (isset($formfields)) {
    FormValidate($form, $errors, $fields, $formfields);
}
else {
    $formfields = null;
}

170
echo "<center>";
171
FormRender($form, $errors, $fields, $formfields);
172 173 174 175 176 177 178
echo "</center>";

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>