Permalink
Browse files

Added profile form example using fieldsets.

  • Loading branch information...
1 parent a3fc40a commit 4e04e259d79eebe5f13d64c56d0ee916e91aa036 @xdissent committed Oct 2, 2009
Showing with 99 additions and 0 deletions.
  1. +99 −0 examples/profile_form.php
@@ -0,0 +1,99 @@
+<?php
+
+error_reporting(6143|2048);
+
+require_once('../src/phorms.php');
+
+function required($value)
+{
+ if ($value == '' || is_null($value))
+ throw new ValidationError('This field is required.');
+}
+
+class ProfileForm extends FieldsetPhorm
+{
+ protected function define_fields()
+ {
+ // Define form fields
+ $this->user_id = new HiddenField(array('required'));
+ $this->first_name = new TextField("First name", 25, 255, array('required'));
+ $this->last_name = new TextField("Last name", 25, 255, array('required'));
+ $this->email = new EmailField("Email address", 25, 255, array('required'));
+ $this->url = new URLField("Home page", 25, 255);
+ $this->bio = new LargeTextField('Bio', 5, 40, array('required'));
+
+ // Add some help text
+ $this->email->set_help_text('We will never give out your email address.');
+ }
+
+ protected function define_fieldsets()
+ {
+ $this->fieldsets = array(new Fieldset('name', 'Name', array('user_id',
+ 'first_name',
+ 'last_name')),
+ new Fieldset('extra', 'Extra', array('email',
+ 'url',
+ 'bio')));
+ }
+
+ public function report()
+ {
+ var_dump( $this->cleaned_data() );
+ }
+}
+
+// Set up the form
+$post_id = 42;
+$form = new ProfileForm(Phorm::POST, false, array('post_id'=>$post_id));
+
+// Check form validity
+$valid = $form->is_valid();
+
+?>
+<html>
+ <body>
+ <style>
+ table { border: 1px solid #ccc; padding: 2px 4px; }
+ th { vertical-align: top; text-align: right; }
+ td { vertical-align: top; }
+ thead th { text-align: center; font-size: 16pt; background-color: #ccc; }
+ .phorm_error { color: #bb0000; font-size: 10pt; text-align: left; font-style: oblique; }
+ .phorm_help { margin: 0; padding: 2px; font-size: 10pt; font-style: oblique; color: #666; }
+ </style>
+
+ <?= $form->open() ?>
+ <table>
+ <thead>
+ <tr><th colspan="2">Add a comment</th></tr>
+ <?php if ( $form->has_errors() ): ?>
+ <tr><th class="phorm_error" colspan="2">Please correct the following errors.</th></tr>
+ <?php endif ?>
+ </thead>
+ <tbody>
+ <?= $form ?>
+ <tr>
+ <th colspan="2">
+ <input type="button" value="Clear form" onclick="javascript:location.href='<?= $_SERVER['PHP_SELF'] ?>'" />
+ <input type="submit" value="Submit" />
+ </th>
+ </tr>
+ </tbody>
+ </table>
+ <?= $form->close() ?>
+
+ <h4>Raw POST data:</h4>
+ <?php var_dump($_POST); ?>
+
+ <hr />
+
+ <?php if ($form->is_bound() && $valid): ?>
+ <h4>Processed and cleaned form data:</h4>
+ <? $form->report() ?>
+ <?php elseif ($form->has_errors()): ?>
+ <h4>Errors:</h4>
+ <?php var_dump($form->get_errors()); ?>
+ <?php else: ?>
+ <p><em>The form is unbound.</em></p>
+ <?php endif ?>
+ </body>
+</html>

0 comments on commit 4e04e25

Please sign in to comment.