/** * Load a model's fields into the form * * @access public * @param mixed Jelly_Model $model * @param mixed array $fields. (default: NULL) * @return form object */ public function load(Jelly_Model $model, array $fields = NULL) { $this->model = $model; $this->make_fields($fields); foreach ($model->meta()->fields() as $column => $field) { if (in_array($column, $this->skip_fields)) { continue; } if ($this->fields and !in_array($column, $this->fields)) { continue; } // Create the array $options = (array) $field; // Fetch the validation key names from the config file $validation_keys = $this->config()->validation_keys; // Look for validation rules as defined by the config file foreach ($validation_keys as $key => $value) { // If they are using the assumed names, do nothing if ($key === $value) { continue; } // Only grab the proper validation settings from jelly field definition $options[$key] = !empty($options[$value]) ? $options[$value] : array(); // No need to carry duplicates for a rule unset($options[$value]); } // Determine the driver $options['driver'] = $this->determine_driver($options, get_class($field)); // Add the value if ($field instanceof Jelly_Field_Relationship === FALSE) { // Add the value $options['value'] = $model->get($column) ? $model->get($column) : $options['default']; } elseif ($field instanceof Field_ManyToMany === FALSE and $field instanceof Field_HasMany === FALSE) { // grab the actual foreign model $foreign_model = $model->get($column)->execute(); // Set the value $options['value'] = $foreign_model->id(); } else { // Grab all the foreign options $all_options = Jelly::select($field->foreign['model'])->execute(); // Create the array $options['options'] = array(); $options['value'] = array(); foreach ($all_options as $option) { // Build the option $options['options'][] = array('value' => $option->id(), 'alias' => $option->name()); if ($model->has($column, $option)) { $options['value'][] = $option->id(); } } } // Add the field to its parent $this->form->add($column, $options); $field = $this->form->{$column}; } return $this->form; }