Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created July 13, 2015 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/c512b4cab88d252e8058 to your computer and use it in GitHub Desktop.
Save tzkmx/c512b4cab88d252e8058 to your computer and use it in GitHub Desktop.
Form Class for Silex
<?php
use Symfony\Component\HttpFoundation\Request;
use Forms\DateRange;
$app->match('/sandbox', function(Request $req) use($app)
{
$form = $app['form.factory']->createNamedBuilder(null, new DateRange())
->getForm();
return $app['twig']->render('sandbox.html',
array('form' => $form->createView(),
'params' => $req->request->get('date_start') . '&' . $req->request->get('date_end'))
);
})->method('GET|POST');
<?php
namespace Forms;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Builds a form to select a date range
*
* @author Jesús Franco Martínez
*/
class DateRange extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
return $builder
->add('date_start', 'date', array(
'required' => true,
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'label' => 'Fecha inicial'
))
->add('date_end', 'date', array(
'required' => true,
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'label' => 'Fecha final'
));
}
public function getName()
{
return 'date_range';
}
}
{% extends "layout.html" %}
{% block title %}date range sandbox{% endblock %}
{% block content %}
<div class="controls">
{{ form_start(form) }}
<span class="row">
{{ form_label(form.date_start) }}
{{ form_errors(form.date_start) }}
{{ form_widget(form.date_start) }}
</span>
<span class="row">
{{ form_label(form.date_end) }}
{{ form_errors(form.date_end) }}
{{ form_widget(form.date_end) }}
</span>
<input type="submit" value="Consulta" />
{{ form_widget(form) }}
{{ form_end(form) }}
</div>
<p>Current parameters: {{ params }}</p>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script>
(function($) {
$.datepicker.setDefaults(
$.extend(
$.datepicker.regional['es'],
{'dateFormat':'yy-mm-dd'}
)
);
$(document).ready(function() {
$( "#date_start" ).datepicker();
$( "#date_end" ).datepicker();
});
})(jQuery);
</script>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment