New Sado Version 1.1 released! Easily add ORM to your applications using our Sado Library

Sado Form Object Documentation

The Sado Form object is used to easily post data to the database and edit data in the database using HTML forms.

Build a Sado Form object is easy. First setup an Sado ORM class, then create a Sado Form class: // include bootstrap
require_once './lib/Sado/sado.bootstrap.php';

// create ORM class
class User extends SadoORM
{
      // nothing here
}

// create Form class
class UserForm extends SadoForm
{
      // initialize the form
      public function __construct()
      {
            // add instance of ORM class
            $this->model(new User);

            // add fields
            $this->field('fullname')
                  ->label('Name:');

            $this->field('email')
                  ->label('Email:');

            // add submit button
            $this->fieldSubmit()
                  ->value('Save');

            // active form controls (required)
            $this->activate();
      }
}

// create instance of form
$form = new UserForm;

// display form
echo $form->getForm();
That's all there is to it, now you can add records to the 'users' table using the form. You could modify the form to edit users like this: [...]
// create Form class
class UserForm extends SadoForm
{
      // initialize the form
      public function __construct($user_id)
      {
            // add instance of ORM class
            $this->model(new User($user_id));
[...]
// create instance of form, load user with ID 5
$form = new UserForm(5);
[...]
This will load the user into the form and now you can edit the user data.


Set Field Default Value

Sometimes default field values are needed, for example the field 'is_active' might be always set to '1' when a user registers. We can set a default value of '1' for the 'is_active' field like this: [...]
// create Form class
class UserForm extends SadoForm
{
      // initialize the form
      public function __construct()
      {
            // set instance of ORM class
            $user = new User;

            // set default value for is_active field (inactive)
            $user->is_active = 0;

            // add instance of ORM class
            $this->model($user);

            // flush field is used for security reasons so that a user cannot create
            // a custom post to this form with the field 'is_active' = 1
            // if this was not set the default value above can be overridden
            $this->flushField('is_active');
[...]


Modify Post Value

You can modify a post value before the value is inserted into the database. For example, you might was to MD5 hash a password before saving it. This can be done using the 'onValidate' method: [...]
// create Form class
class UserForm extends SadoForm
{
      // initialize the form
      public function __construct()
      {
            // add instance of ORM class
            $this->model(new User);
[...]
      // add onValidate method, this will be called after validation is successful
      protected function onValidate()
      {
            // MD5 hash the user's password
            $hashed_pwd = md5( $this->field('email')->getValue() );

            // modify post value so hashed password will be stored in database
            $this->field('email')->value( $hashed_pwd );
      }
[...]


Remove ORM Field

Sometimes you might want to remove a field from the ORM object you are using for your form so it doesn't save any value (for example you might have a default value for a field setup in the database table and don't want a NULL value overriding the default value), here is an example of how to remove a field: [...]
// create Form class
class UserForm extends SadoForm
{
      // initialize the form
      public function __construct()
      {
            // set instance of ORM class
            $user = new User;

            // set default value for is_active field
            $user->is_active = 1;

            // remove field 'date_created' (this field uses the default value for now)
            $user->removeField('date_created');

            // add instance of ORM class
            $this->model($user);
[...]