From 688925acc03820dcbf62cceec26e094d7b4fc223 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 5 May 2015 23:24:37 +0200 Subject: [PATCH] form --- app/locales/en.yml | 5 + src/Gist/Controller/HomeController.php | 15 +- src/Gist/Form/AbstractForm.php | 4 +- src/Gist/Form/CreateGistForm.php | 8 +- src/Gist/Resources/views/Home/index.html.twig | 168 +++++++++--------- src/Gist/Resources/views/base.html.twig | 14 +- web/app/css/app.css | 3 +- web/app/js/app.js | 30 +++- 8 files changed, 145 insertions(+), 102 deletions(-) diff --git a/app/locales/en.yml b/app/locales/en.yml index caf64de..cb42d79 100644 --- a/app/locales/en.yml +++ b/app/locales/en.yml @@ -8,13 +8,18 @@ app: title: 'About' form: + error: + not_blank: 'This value should not be blank bro!' title: placeholder: 'Title' cipher: + alert: 'By enabling cipher, fork will be not possible.' + label: 'Cipher: %value%' choice: yes: 'Yes' no: 'No' type: + label: 'Language: %value%' choice: xml: 'HTML/XML' css: 'CSS' diff --git a/src/Gist/Controller/HomeController.php b/src/Gist/Controller/HomeController.php index 638740f..0ec2735 100644 --- a/src/Gist/Controller/HomeController.php +++ b/src/Gist/Controller/HomeController.php @@ -14,9 +14,22 @@ class HomeController { public function indexAction(Request $request, Application $app) { - $form = new CreateGistForm($app['form.factory'], $app['translator']); + $data = array( + 'type' => 'xml', + 'cipher' => 'no', + ); + + $form = new CreateGistForm($app['form.factory'], $app['translator'], $data); $form = $form->build(); + if ($request->isMethod('post')) { + $form->submit($request); + + if ($form->isValid()) { + + } + } + return $app['twig']->render( 'Home/index.html.twig', array( diff --git a/src/Gist/Form/AbstractForm.php b/src/Gist/Form/AbstractForm.php index 220d7d4..cee27f8 100644 --- a/src/Gist/Form/AbstractForm.php +++ b/src/Gist/Form/AbstractForm.php @@ -15,11 +15,11 @@ abstract class AbstractForm protected $translator; - public function __construct(FormFactory $formFactory, Translator $translator) + public function __construct(FormFactory $formFactory, Translator $translator, array $data = array()) { $this->translator = $translator; - $this->builder = $formFactory->createBuilder('form'); + $this->builder = $formFactory->createBuilder('form', $data); } abstract public function build(array $options = array()); diff --git a/src/Gist/Form/CreateGistForm.php b/src/Gist/Form/CreateGistForm.php index f2cd917..a115028 100644 --- a/src/Gist/Form/CreateGistForm.php +++ b/src/Gist/Form/CreateGistForm.php @@ -34,7 +34,9 @@ class CreateGistForm extends AbstractForm 'rows' => 10, ), 'constraints' => array( - new NotBlank(), + new NotBlank(array( + 'message' => $this->translator->trans('form.error.not_blank'), + )), ), ) ); @@ -57,8 +59,8 @@ class CreateGistForm extends AbstractForm array( 'required' => true, 'choices' => array( - 0 => $this->translator->trans('form.cipher.choice.no'), - 1 => $this->translator->trans('form.cipher.choice.yes'), + 'no' => $this->translator->trans('form.cipher.choice.no'), + 'yes' => $this->translator->trans('form.cipher.choice.yes'), ), ) ); diff --git a/src/Gist/Resources/views/Home/index.html.twig b/src/Gist/Resources/views/Home/index.html.twig index 2cc91ca..3c3d739 100644 --- a/src/Gist/Resources/views/Home/index.html.twig +++ b/src/Gist/Resources/views/Home/index.html.twig @@ -1,91 +1,85 @@ {% extends 'base.html.twig' %} {% block body %} -
- {{ form(form) }} -
-
-
- -
-
-

- - En activant le chiffrement, le code déposé ne pourra pas être forké. -

-
-
-
- - -
-
- - -
-
-
-

- -

-

- -

-
-
-
-
+
+
+
+
+
+ {{ form_widget(form.title) }} +
+
+

+ + {{ 'form.cipher.alert'|trans }} +

+
+
+
+ + +
+
+ + +
+
+
+

+ {{ form_errors(form.content) }} + {{ form_widget(form.content) }} +

+

+ +

+
+
+
+ {{ form_widget(form._token) }} +
+
{% endblock %} diff --git a/src/Gist/Resources/views/base.html.twig b/src/Gist/Resources/views/base.html.twig index 0c42131..29ad64f 100644 --- a/src/Gist/Resources/views/base.html.twig +++ b/src/Gist/Resources/views/base.html.twig @@ -42,13 +42,13 @@
- {% block body %}{% endblock %} - {% block js %} - - - - + + + + {% endblock %} diff --git a/web/app/css/app.css b/web/app/css/app.css index 1cb1e71..8bd04f7 100644 --- a/web/app/css/app.css +++ b/web/app/css/app.css @@ -12,7 +12,7 @@ body { border: #333; } -#code { +#form_content { display: block; width: 100%; padding: 10px; @@ -43,3 +43,4 @@ body { .panel-heading .actions { margin-top: -5px; } + diff --git a/web/app/js/app.js b/web/app/js/app.js index df3725a..de724be 100644 --- a/web/app/js/app.js +++ b/web/app/js/app.js @@ -1,3 +1,31 @@ $('textarea').on('keyup change', function() { - $(this).attr('rows', Math.max(10, $(this).val().split("\n").length)); + $(this).attr('rows', Math.max(10, $(this).val().split("\n").length)); +}); + +$('#options input').change(function() { + var $input = $(this); + + var $label = $($input.data('id')); + + $label.html($label.data('tpl').replace('%value%', $input.data('title'))); +}); + +$('#options li, #options a').click(function() { + $(this).find('label').trigger('click'); +}); + +$('#options label').click(function(e) { + e.stopPropagation(); +}); + +$('#options input:checked').each(function() { + $(this).trigger('change'); +}); + +$('.cipher-input').change(function() { + if ($('.cipher-input:checked').val() === 'yes') { + $('#cipher-alert').removeClass('hide'); + } else { + $('#cipher-alert').addClass('hide'); + } });