/**
  * Prepare form
  *
  * @param array $config
  * @param array $defaults
  * @return Rabbit_Form
  */
 public function prepare_form(array $config, array $defaults = array(), $id = '')
 {
     //create form
     $form = Rabbit_Form_Factory::factory($config['form']['type'], $config['table']);
     $form->setGenerateAssets($config['form']['automatic_assets']);
     $form->setPrimaryKey($config['primary_key']);
     $form->setEditId($id);
     //form validator
     if (isset($config['form']['validation'])) {
         $form->setValidationCallback($config['form']['validation']);
     }
     //form events
     if (isset($config['form']['preinsert'])) {
         $form->setPreInsert($config['form']['preinsert']);
     }
     if (isset($config['form']['postinsert'])) {
         $form->setPostInsert($config['form']['postinsert']);
     }
     if (isset($config['form']['preupdate'])) {
         $form->setPreUpdate($config['form']['preupdate']);
     }
     if (isset($config['form']['postupdate'])) {
         $form->setPostUpdate($config['form']['postupdate']);
     }
     if (isset($config['form']['prechange'])) {
         $form->setPreChange($config['form']['prechange']);
     }
     if (isset($config['form']['postchange'])) {
         $form->setPostChange($config['form']['postchange']);
     }
     if (isset($config['form']['predelete'])) {
         $form->setPreDelete($config['form']['predelete']);
     }
     if (isset($config['form']['postdelete'])) {
         $form->setPostDelete($config['form']['postdelete']);
     }
     //parse hiddens
     if (isset($config['form']['hidden'])) {
         foreach ($config['form']['hidden'] as $name => $value) {
             $form->addHiddenField($name, $value);
         }
     }
     //add hidden form indentifier
     $form->addHiddenField('rabbit-form-id', $this->getFormIdentifier($config));
     //parse fields
     foreach ($config['fields'] as $name => $field) {
         //get params
         $params = isset($field['params']) ? $field['params'] : array();
         //initialize field
         $f = Rabbit_Field_Factory::factory($field['type'], $form, $params);
         $f->setName($name);
         $f->setLabel($field['label']);
         //set persist
         if (isset($field['persist'])) {
             $f->setPersist($field['persist']);
         }
         //populate field
         if (isset($_POST[$name])) {
             //check for post repopulate
             $f->setValue($_POST[$name]);
         } elseif (isset($defaults[$name])) {
             //check for predefined repopulate
             $f->setRawValue($defaults[$name]);
         } elseif (isset($field['value'])) {
             //check for config repopulate
             $f->setValue($field['value']);
         }
         //validators
         if (isset($field['validators'])) {
             foreach ($field['validators'] as $validator) {
                 $v = Rabbit_Validator_Factory::factory($validator['type'], $f);
                 if (isset($validator['params'])) {
                     $v->setParams($validator['params']);
                 }
             }
         }
         //initialize field
         $f->initialize();
     }
     return $form;
 }
Example #2
0
 /**
  * Overload the construtor to force image extensions
  *
  * @param Rabbit_Form $form
  * @param array $attributes
  */
 public function __construct(Rabbit_Form $form, array $attributes = array())
 {
     parent::__construct($form, $attributes);
     $validator = Rabbit_Validator_Factory::factory('File', $this);
     $validator->setParams(array('extensions' => array('jpg', 'jpeg', 'gif', 'png', 'bmp')));
 }