Development

sfDynamicsFormBuilderPlugin

You must first sign up to be able to contribute.

Symfony Dynamics Form Builder Plugin

Dynamically generate form based on the model. Form rendering options are stored in schema.yml and validation information are stored in symfony build-in Validation File.

Usage

File: schema.yml (Need yml format, will not work on xml format)

propel:
  sf_guard_user_profile: 
    _attributes:                { idMethod: native }
    id:                         { type: INTEGER, required: true, autoIncrement: true, primaryKey: true }
    user_id:                    { type: INTEGER, required: true, foreignTable: sf_guard_user, foreignReference: id, onDelete: cascade }
    last_name:                  { type: VARCHAR, size: 128 }
    given_name:                 { type: VARCHAR, size: 256 }
    initial:                    { type: VARCHAR, size: 4, f_label: 'Your Initial in Publication' }
    email:                      { type: VARCHAR, size: 256 }
    phone:                      { type: VARCHAR, size: 32 }
    mobile:                     { type: VARCHAR, size: 32 }
    country:                    { type: VARCHAR, size: 64 }
    company:                    { type: VARCHAR, size: 128 }
    title:                      { type: VARCHAR, size: 128 }
    subtitle:                   { type: VARCHAR, size: 128 }
    postcode:                   { type: VARCHAR, size: 16 }
    state:                      { type: VARCHAR, size: 64 }
    publicview:                 { type: BOOLEAN, default: 0, f_display_type: checkbox }
    retrieve_publication:       { type: BOOLEAN, default: 0, f_display_type: checkbox }
    allow_account_sitting:      { type: BOOLEAN, default: 0, f_display_type: checkbox }
    aboutmyself:                { type: LONGVARCHAR, f_display_type: text, f_label: 'Something about Yourself' }
    photo:                      { type: VARCHAR, size: 256, f_display_type: image }
    updated_at:                 { type: TIMESTAMP, f_display_type: time }
    position:                   { type: LONGVARCHAR, f_display_type: text }
    research:                   { type: LONGVARCHAR, f_display_type: text }
    grants:                     { type: LONGVARCHAR, f_display_type: text }
    awards:                     { type: LONGVARCHAR, f_display_type: text }    
    organization_group_id:      { type: INTEGER, required: true, foreignTable: organization_group, foreignReference: id, onDelete: cascade, displayValue: title, f_label: 'Primary Organization Group' }

Keywords

  • f_readonly: true, false
    The field is readonly. Can not be changed in the form.
  • f_skip: true, false
    Skip the field. Foreign Key, CreatedAt? and UpdatedAt? field are skip by default.
  • f_display_type: date, time, text, checkbox, textfield, image
    Display type of the field. textfield is the default option.
    text is rich textarea.
    Field name id is hidden text field by default.
    Field type of boolean is a checkbox by default.
  • f_label:
    Custom Label for the field.
  • f_class:
    Custom CSS class for the field.

NOTE: If foreign key have displayValue, then it is not skip.

File: action.php

  public function executeProfileEdit() {
    /** @var sfGuardUser **/
    $user = $this->getUser()->getGuardUser();
    $formname = "user_profile";
    $fbuilder = new sfDynamicsFormBuilder($formname, "home/profileSave", $user->getProfile());
    $fbuilder->setSkipField(array(SfGuardUserProfilePeer::ID));
    $fbuilder->setValidationInfo("home", "profileSave");
    $fbuilder->init();
    $this->builder = $fbuilder;
  }

  public function executeProfileSave() {
    /** @var sfGuardUser **/
    $user = $this->getUser()->getGuardUser();
    /** @var sfGuardUserProfile **/
    $profile = $user->getProfile();
    $request_array = $this->getRequest()->getParameterHolder()->getAll();
    $profile->fromArray($request_array, BasePeer::TYPE_FIELDNAME);
    $profile->setId($id);
    $profile->setsfGuardUser($user);
    if ($this->getRequest()->hasFiles()) {
      $fileName = $this->getRequest()->getFileNames();
      $uploadDir = sfConfig::get('sf_upload_dir');
      $filename = 'user'.$user->getId().'profilephoto.pic';
      $this->getRequest()->moveFile($fileName[0], $uploadDir.'/'.$filename);
      $profile->setPhoto($filename);
    }
    if ($profile->validate()) {
      $profile->save();
      $this->redirect("home/index");
    } else {
      $this->redirect("home/profileEdit");
    }
  }

We are skipping primary field because this is a user profile form and its primary field does not and should not change.

File: profileEditSuccess.php

<?php
  use_helper('Javascript', 'sfDynamicsFormBuilder');
?>
<?php echo sfDFB_form_tag($builder); ?>
<?php if ($sf_request->hasErrors()): ?>
  <p>The data you entered seems to be incorrect.
  Please correct the following errors and resubmit:</p>
  <ul>
  <?php foreach($sf_request->getErrors() as $name => $error): ?>
    <li><?php echo $name ?>: <?php echo $error ?></li>
  <?php endforeach; ?>
  </ul>
<?php endif; ?>
<table>
  <tbody>
  <?php
    while ($builder->nextAvailable()) {
      if ($builder->visible()) {
  ?>
  <tr>
    <th><?php echo $builder->getLabel() ?></th>
    <td><?php echo $builder->getColumnField(); echo $builder->getError(); ?></td>
  </tr>
  <? 
      } else {
        echo $builder->getColumnField();
      }
      $builder->nextColumn();
    } 
  ?>
  </tbody>
</table>
<hr />
<?php echo submit_tag('save') ?>
</form>

<?php echo init_dynamics_form_builder($builder); ?>

sfMixer Extensibility

sfDynamicsFormBuilder:getColumnField:valueModifier - Modify the value before output.

  • Parameters:
    • ColumnMap? Object currently processing
    • String Value of the Column.
  • Return String to set the value of the column.

sfDynamicsFormBuilder:getColumnField:linkCriteriaModifier - Give the ability to modify the criteria used for generating Foreign Key drop down box.

  • Parameters:
    • ColumnMap? Object currently processing
    • Criteria Criteria use to filter the query.
  • Return Criteria.

SVN located at sfDynamicsFormBuilder Repository

Attachments