Ejemplo n.º 1
0
 /**
  *  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;
 }
Ejemplo n.º 2
0
    public static function fetch($form_name)
    {
        $filename = dirname(__DIR__) . '/forms/' . $form_name . '.yaml';
        if (file_exists($filename)) {
            return YAML::parse(file_get_contents($filename));
        } else {
            $error = 'missing form: ' . $filename;
            throw new Exception($error);
        }
    }
}
// GET /jobs
$app->get('/', function (Application $app) {
    return $app['mustache']->render('index.mustache', array('forms' => array(array('name' => 'Base Example', 'url' => '/forms/base'))));
});
$app->match('/forms/base', function (Application $app, Request $request) {
    $form = Acreage\Form::create_from_data(array('validator' => $app['validator']), FormConfig::fetch('base'));
    $form->handle_request($request);
    $render_context = Acreage\Render\Context::create($form);
    $render_context['content'] = implode('', array_map(function ($field) use($app) {
        if (in_array($field['type'], array_keys(Acreage\Form::$field_type_map))) {
            return $app['mustache']->render('form/' . $field['type'] . '.mustache', $field);
        } else {
            return $app['mustache']->render('form/input.mustache', $field);
        }
    }, $render_context['fields']));
    $render_context['invalid'] = $render_context['validated'] && !$render_context['valid'];
    $render_context['success'] = $render_context['validated'] && $render_context['valid'];
    return $app['mustache']->render('form.mustache', $render_context);
});
return $app;
Ejemplo n.º 3
0
 /**
  * @covers Acreage\Render\Context::create()
  */
 public function test_create_returns_context()
 {
     $mock_config = array('validator' => 'validator');
     $subject = Form::create_from_data($mock_config, array('fields' => array(array('name' => 'field1', 'type' => 'text'))));
     $this->assertSame(array('attributes' => array('method' => 'POST'), 'fields' => array(array('name' => 'field1', 'type' => 'text', 'value' => null, 'label' => false, 'valid' => null, 'errors' => array(), 'validated' => false)), 'valid' => false, 'validated' => false), Context::create($subject));
 }
Ejemplo n.º 4
0
 /**
  * @covers Acreage::Form::add_field
  */
 public function test_add_field_adds_correct_type_for_files()
 {
     $mock_config = array('validator' => 'mock.validator');
     $subject = new Form($mock_config);
     $subject->add_field('field', 'file');
     $this->assertInstanceOf('Acreage\\Fields\\File', $subject->fields[0]);
 }