Last active
August 29, 2015 14:08
-
-
Save webdevilopers/869e22a95ea0abb9195c to your computer and use it in GitHub Desktop.
Uniform date picker and form element in symfony depending on locale resp. default_locale
This file contains hidden or 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
| framework: | |
| default_locale: de | |
| translator: { fallback: "%locale%" } |
This file contains hidden or 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 "AcmeDemoBundle::layout.html.twig" %} | |
| {% block content %} | |
| <script> | |
| $(function() { | |
| $(".date").datepicker({ showWeek: true, maxDate: new Date, beforeShowDay: function(date){ return [date.getDay() == 1, ""] } }); | |
| }); | |
| </script> | |
| {{ form(form) }} | |
| {% endblock %} |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta name="robots" content="noindex,nofollow" /> | |
| <link href="http://localhost:8000/bundles/framework/css/structure.css" rel="stylesheet" /> | |
| <link href="http://localhost:8000/bundles/framework/css/body.css" rel="stylesheet" /> | |
| <link rel="icon" sizes="16x16" href="/favicon.ico" /> | |
| <link rel="stylesheet" href="/bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css" /> | |
| <script src="/bundles/sonatacore/vendor/jquery/dist/jquery.min.js" type="text/javascript"></script> | |
| <script src="/bundles/sonataadmin/vendor/jqueryui/ui/minified/jquery-ui.min.js" type="text/javascript"></script> | |
| <script src="/bundles/sonataadmin/vendor/jqueryui/ui/minified/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script> | |
| <script type="text/javascript"> | |
| $(function() { | |
| $.datepicker.setDefaults($.datepicker.regional['de']); | |
| $.datepicker.setDefaults({ showWeek: true, firstDay: 1 }); | |
| }); | |
| </script> | |
| </head> | |
| <div class="block"> | |
| <script> | |
| $(function() { | |
| $(".date").datepicker({ showWeek: true, maxDate: new Date, beforeShowDay: function(date){ return [date.getDay() == 1, ""] } }); | |
| }); | |
| </script> | |
| <form name="timekeeping" method="post" action=""><div id="timekeeping"> | |
| <div> | |
| <label for="timekeeping_date" class="required">Date</label><input type="text" id="timekeeping_date" name="timekeeping[date]" required="required" class="date" value="06.10.2014" /></div><div> | |
| <button type="submit" id="timekeeping_submit" name="timekeeping[submit]">Submit</button></div><input type="hidden" id="timekeeping__token" name="timekeeping[_token]" value="j2VYRJX4mmGhfgFi0Rawi7yBX3uGJKFBTqo4rYHtUPA" /></div></form> | |
| </div> |
This file contains hidden or 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 "TwigBundle::layout.html.twig" %} | |
| {% block head %} | |
| <link rel="icon" sizes="16x16" href="{{ asset('favicon.ico') }}" /> | |
| <link rel="stylesheet" href="{{ asset('bundles/acmedemo/css/demo.css') }}" /> | |
| <link rel="stylesheet" href="{{ asset('/bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css') }}" /> | |
| <script src="/bundles/sonatacore/vendor/jquery/dist/jquery.min.js" type="text/javascript"></script> | |
| <script src="/bundles/sonataadmin/vendor/jqueryui/ui/minified/jquery-ui.min.js" type="text/javascript"></script> | |
| <script src="/bundles/sonataadmin/vendor/jqueryui/ui/minified/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script> | |
| <script type="text/javascript"> | |
| $(function() { | |
| $.datepicker.setDefaults($.datepicker.regional['{{ app.request.locale }}']); | |
| $.datepicker.setDefaults({ showWeek: true, firstDay: 1 }); | |
| }); | |
| </script> | |
| {% endblock %} | |
| {% block body %} | |
| {% endblock %} |
This file contains hidden or 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
| parameters: | |
| locale: de |
This file contains hidden or 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 Acme\DemoBundle\Controller; | |
| use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
| use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
| use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
| use Symfony\Component\HttpFoundation\Request; | |
| use Symfony\Component\HttpFoundation\Response; | |
| use Acme\DemoBundle\Entity\TimekeepingEntry; | |
| use Acme\DemoBundle\Form\TimekeepingEntryType; | |
| class TimekeepingController extends Controller | |
| { | |
| /** | |
| * @Route("/timekeeping/edit/{id}") | |
| * @Template() | |
| */ | |
| public function editAction(Request $request, TimekeepingEntry $timekeepingEntry) | |
| { | |
| $form = $this->createForm(new TimekeepingEntryType(), $timekeepingEntry); | |
| $form->handleRequest($request); | |
| if ($form->isValid()) { | |
| echo "VALID"; | |
| } else { | |
| echo "INVALID"; | |
| echo "<pre>"; | |
| $errors = $form->getErrors(true); | |
| echo "</pre>"; | |
| } | |
| return array( | |
| 'form' => $form->createView() | |
| ); | |
| } | |
| } |
This file contains hidden or 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 Acme\DemoBundle\Form; | |
| use Symfony\Component\Form\AbstractType; | |
| use Symfony\Component\Form\FormBuilderInterface; | |
| use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
| class TimekeepingEntryType extends AbstractType | |
| { | |
| /** | |
| * @param FormBuilderInterface $builder | |
| * @param array $options | |
| */ | |
| public function buildForm(FormBuilderInterface $builder, array $options) | |
| { | |
| $builder | |
| ->add('date', 'date', array( | |
| // 'widget' => 'choice', // Valid | |
| 'widget' => 'single_text', // Invalid for dd.mm.YYYY when not using format | |
| 'format' => 'dd.MM.yyyy', | |
| 'attr' => array('class' => 'date') | |
| )) | |
| ; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment