/** * create the form from an array of data */ public static function create_from_data($config, $data) { $form = new Form($config); if (array_key_exists('attributes', $data)) { $form->set_attributes($data['attributes']); } if (array_key_exists('defaults', $data)) { $form->set_defaults($data['defaults']); } foreach ($data['fields'] as $field_src) { $field = array_merge(array('options' => array(), 'constraints' => array()), $field_src); $form->add_field($field['name'], $field['type'], $field['options'], $field['constraints']); } return $form; }
/** * @covers Acreage::Form::handle_request */ public function test_handle_request_runs_validation_on_fields_with_matching_method_and_is_invalid_if_invalid() { $mock_config = array('validator' => new MockValidator(array('two' => array('invalid')))); $subject = new Form($mock_config); $request = new Request(array('one' => 'one', 'two' => 'two')); $resp = $subject->set_attributes(array('method' => 'GET'))->add_field('one', 'text')->add_field('two', 'text')->handle_request($request); $this->assertFalse($subject->valid()); $this->assertTrue($subject->request_handled); $this->assertSame($subject, $resp); foreach ($subject->fields as $field) { $this->assertTrue($field->validated); if ($field->name == "one") { $this->assertTrue($field->valid); } else { $this->assertFalse($field->valid); } } }