Created
July 13, 2015 18:03
-
-
Save tzkmx/c512b4cab88d252e8058 to your computer and use it in GitHub Desktop.
Form Class for Silex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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