SXForm sample

The forms component of the Sinax-Framework is designed to facilitate working with forms and fields.

Below is a sample that you can also find in the Sinax-Framework source tree. It creates a simple form containing three fields, two regular SXTextField and one SXEmailField.

The first SXTextField and the SXEmailField are required, the second SXTextField isn’t. The method $form->isValid() will check whether all fields in the form are valid. After the form is valid the submitted values are simply echoed to the user.

This short sample demonstrates how easy it is to use the forms component of the Sinax-Framework and amount of work it takes from your hands.

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
require('sx/forms.inc.php');
 
class MyForm extends SXForm {
	public function __construct() {
		parent::__construct('Verzenden', '');
		$this->addField(new SXTextField('Firstname', 'first_name', true));
		$this->addField(new SXTextField('Lastname', 'last_name', false));
		$this->addField(new SXEmailField('Email', 'email', true, "sample@sinax.be"));
	}
}
 
$form = new MyForm();
 
echo '<h1>SXForm sample: form1.php</h1>';
echo '<p>Required fields: Firstname and email</p>';
 
// Check for POST method
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	// Load post data into the form
	$form->load();
 
	if ($form->isValid()) {
		// Get object from form
		$o = $form->getObject();
		echo 'I got these values: <br />';
		echo 'Firstname: ' . $o->first_name . "<br />";
		echo 'Lastname: ' . $o->last_name . "<br />";
		echo 'Email: ' . $o->email . "<br />";
		$form->clear();
	} else {
		echo "There are errors in your submission! Please correct them!";
	}
}
$form->display();

Leave a Reply

Designed by Structured Settlement | Download from Wordpress templates | Music text | Cheap domain