/** * @dataProvider provider_form */ public function test_form(array $args, array $array) { $expected = TRUE; $result = TRUE; $form = Formo::form($args); foreach ($array as $key => $value) { if ($form->get($key) !== $value) { $result = FALSE; } } $this->assertSame($expected, $result); }
public function action_update() { $element = ORM::Factory($this->_orm_model, $_GET['id']); $form = Formo::form()->orm('load', $element); $form->add('update', 'submit', 'Save'); if ($form->load($_POST)->validate()) { if ($this->_update_passed($form, $element)) { $element->save(); $form->orm('save_rel', $element); $this->request->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller()))); } } else { $this->_update_failed($form, $element); } return $this->render('update', array('form' => $form)); }
/** * Create a form from an ORM Model * You can optionally pass a form to have fields added to * * @access public * @param array $fields (default: NULL) * @param Formo $form (default: NULL) * @return Formo */ public function get_form(array $fields, Formo $form = NULL) { if (Arr::get($fields, 0) === '*') { // Get the default set of fields // By default, load all the fields $arr = array_keys($this->as_array()); if (count($fields) > 1) { // If other fields are listed, add them to the array // this allows for something like ['*', 'foo', 'bar'] array_shift($fields); $arr = Arr::merge($arr, $fields); } // Set the fields to the combined array $fields = $arr; } if ($form === NULL) { // If a form object wasn't passed as the second argument, // create that form object here $alias = $this->_formo_alias ?: $this->_object_name; $form = Formo::form(['alias' => $alias]); } // Set up foreign keys map for easy access $this->_find_foreign_keys(); // Now we iterate through each field foreach ($fields as $key => $field_name) { if (is_array($field_name)) { $method = 'formo_' . $key; if (strpos($method, '.') !== FALSE) { $method = str_replace('.', '_', $method); } $rs = $this->{$method}(); if ($rs instanceof Database_Result) { $model = $rs->current(); $blueprint = $model->get_form($field_name)->set('alias', $key)->set('blueprint', true)->pad_blueprint($rs); $form->add($blueprint); continue; } elseif ($rs instanceof ORM) { $subform = $rs->get_form($field_name); $form->add($subform); continue; } } if ($this->_field_exists($field_name) !== TRUE) { // Custom method looks like _formo_$field_name() $method = '_formo_' . $field_name; if (!method_exists($this, $method)) { // Throw an exception if a field is requested but there's no method defining its options array throw new Kohana_Exception('Formo custom field method, :method, does not exist.', [':method' => __CLASS__ . '::' . $method . '()']); } $options = $this->{$method}(); } else { // Create the field definition array $options = ['alias' => $field_name, 'val' => $this->{$field_name}, 'driver' => 'input']; // Do any special processing if the field is a relational field $this->_process_belongs_to($field_name, $options); $this->_process_has_one($field_name, $options); $this->_process_enum($field_name, $options); $this->_process_set($field_name, $options); } // Add the field to the form $form->add($options); } if ($form->config('model_base_rules') === TRUE) { // If form is set up to include basic MySQL mimicking validation rules // then figure them out $rules = $this->_get_base_rules(); } // Add Model defined rules to base rules $rules = isset($rules) ? Arr::merge($rules, $this->rules()) : $this->rules(); if ($filters = $this->filters()) { foreach ($filters as $alias => $_filters) { // Add filters as well $form->merge($alias, ['filters' => $_filters]); } } // Add the rules to their respective fields $form->add_rules_fields($rules); $this->formo($form); // Load defaults $form->ftype(); return $form; }
/** * Create a subform from fields already in the Container object * * @access public * @param mixed $alias * @param mixed $driver * @param mixed array $fields * @param mixed $order. (default: NULL) * @return object */ public function create_sub($alias, $driver, array $fields, $order = NULL) { // Create the empty subform object $subform = Formo::form($alias, $driver); foreach ($fields as $key => $field) { if (is_string($key) and !ctype_digit($key)) { // Pull fields "as" a new alias $new_alias = $field; $field = $key; } // Find each field $_field = $this->find($field); if (!$_field) { // Throw an exception if the field doesn't exist throw new Kohana_Exception("Formo_Container: Field {$field} is not in form"); } if (!empty($new_alias)) { // Set the new alias $_field->alias($new_alias); } // Remember the field's original parent $last_parent = $_field->parent(); // Add the field to the new subform $subform->append($_field); // Remove the field from its original parent $last_parent->remove($_field->alias()); } // If the parent has a model, copy it to the new subform $subform->set('model', $this->get('model')); $order and $subform->set('order', $order); // Append the new subform $this->append($subform); return $this; }
/** * Create a copy from form or group object * * @access public * @param array $vals (default: array()) * @return Formo */ public function copy_blueprint($vals = array()) { // The copy is a new formo object with the count of blueprint copies as its alias $blueprint_copy = Formo::form(array('alias' => $this->_blueprint_count, 'blueprint_key' => $this->_blueprint_count)); $attrs = array_combine(static::$_to_array_attrs, static::$_to_array_attrs); $attrs['blueprint_template'] = 'blueprint_template'; unset($attrs['alias']); unset($attrs['driver']); unset($attrs['fields']); $array = $blueprint_copy->to_array($attrs); if ($blueprint_template = $this->get('blueprint_template')) { // Look for blueprint_template and set it as the template for each blueprint copy $array['template'] = $blueprint_template; } $blueprint_copy->set($array); // Keep track how many blueprints copies have been made $this->_blueprint_count++; if (!empty($vals) and $vals instanceof ORM) { // Look for ORM object and cast it as an array $vals = $vals->as_array(); } foreach ($this->_fields as $field) { if ($this->_blueprint_count > 0 and is_int($field->alias())) { // Don't include other blueprint copies in the fields continue; } $attrs = array_combine(static::$_to_array_attrs, static::$_to_array_attrs); unset($attrs['fields']); $array = $field->to_array($attrs); $alias = $field->alias(); if (isset($vals[$alias])) { $array['val'] = $vals[$alias]; } // Add field definition to blueprint $blueprint_copy->add($array); } return $blueprint_copy; }