1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
<?php
/**
* Nette Forms manual form rendering.
*/
if (@!include 'Nette/loader.php') {
die('Install packages using `composer install`');
}
use Nette\Forms\Form;
use Tracy\Debugger;
use Tracy\Dumper;
Debugger::enable();
$form = new Form;
$form->addText('name')
->setRequired('Enter your name');
$form->addText('age')
->setRequired('Enter your age');
$form->addRadioList('gender', NULL, [
'm' => 'male',
'f' => 'female',
]);
$form->addText('email')
->setRequired(FALSE)
->addRule($form::EMAIL, 'Incorrect email address');
$form->addSubmit('submit');
if ($form->isSuccess()) {
echo '<h2>Form was submitted and successfully validated</h2>';
Dumper::dump($form->getValues());
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Nette Forms manual form rendering</title>
<link rel="stylesheet" media="screen" href="assets/style.css" />
<script src="https://nette.github.io/resources/js/netteForms.js"></script>
</head>
<body>
<h1>Nette Forms manual form rendering</h1>
<?php $form->render('begin') ?>
<?php if ($form->errors): ?>
<ul class="error">
<?php foreach ($form->errors as $error): ?>
<li><?php echo htmlspecialchars($error) ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<fieldset>
<legend>Personal data</legend>
<table>
<tr class="required">
<th><?php echo $form['name']->getLabel('Your name:') ?></th>
<td><?php echo $form['name']->control->cols(35) ?> <?php echo $form['name']->error ?></td>
</tr>
<tr class="required">
<th><?php echo $form['age']->getLabel('Your age:') ?></th>
<td><?php echo $form['age']->control->cols(5) ?> <?php echo $form['age']->error ?></td>
</tr>
<tr>
<th><?php echo $form['gender']->getLabel('Your gender:') ?></th>
<td><?php echo $form['gender']->control ?> <?php echo $form['gender']->error ?></td>
</tr>
<tr>
<th><?php echo $form['email']->getLabel('Email:') ?></th>
<td><?php echo $form['email']->control->cols(35) ?> <?php echo $form['email']->error ?></td>
</tr>
</table>
</fieldset>
<div>
<?php echo $form['submit']->getControl('Send') ?>
</div>
<?php $form->render('end'); ?>
</body>
</html>
|