Created
November 21, 2012 09:40
-
-
Save chanmix51/4124003 to your computer and use it in GitHub Desktop.
Form class with Pomm & Silex
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 | |
// ... | |
$app->get('/{slug}', function($slug) use ($app) { | |
$post = $app['pomm.connection'] | |
->getMapFor('Model\SoniaCosta\Sc\Post') | |
->findBySlugWithComments($slug) | |
; | |
if (!$post) | |
{ | |
return new Response(sprintf("No such post '%s'.", $slug), 404); | |
} | |
$form_view = $app['form.factory'] | |
->createBuilder(new \Form\Comment()) | |
->getForm() | |
->createView(); | |
return $app['twig']->render("show_post.html.twig", array('post' => $post, 'comment_form' => $form_view)); | |
})->bind('post_show'); |
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 | |
// ... | |
$app->post('/{slug}/comment/add', function($slug) use ($app) { | |
$post = $app['pomm.connection'] | |
->getMapFor('Model\SoniaCosta\Sc\Post') | |
->findBySlugWithComments($slug) | |
; | |
if (!$post) | |
{ | |
return new Response(sprintf("No such post '%s'.", $slug), 404); | |
} | |
$form = $app['form.factory'] | |
->createBuilder(new \Form\Comment()) | |
->getForm() | |
->bind($app['request']); | |
if ($form->isValid()) | |
{ | |
$values = $form->getData(); | |
$values['post_slug'] = $post->getSlug(); | |
$comment = $app['pomm.connection'] | |
->getMapFor('Model\SoniaCosta\Sc\Comment') | |
->createAndSaveObject($values); | |
return $app->redirect($app['url_generator']->generate('post_show', array('slug' => $post->getSlug()))); | |
} | |
return $app['twig']->render("show_post.html.twig", array('post' => $post, 'comment_form' => $form->createView())); | |
})->bind('new_comment'); |
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 // sources/lib/Form/Comment.php | |
namespace Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Symfony\Component\Validator\Constraints; | |
class Comment extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
return $builder->add('author_name', 'text', array('constraints' => new Constraints\NotBlank(array('message' => 'Champs obligatoire')))) | |
->add('author_email', 'text', array('constraints' => array(new Constraints\NotBlank(array('message' => 'Champs obligatoire')), new Constraints\Email(array('message' => 'Email invalide'))))) | |
->add('author_website', 'text', array('constraints' => array(new Constraints\Url(array('message' => 'URL invalide'))))) | |
->add('title', 'text', array('constraints' => new Constraints\NotBlank(array('message' => 'Champs obligatoire')))) | |
->add('content', 'textarea', array('constraints' => new Constraints\NotBlank(array('message' => 'Champs obligatoire')))); | |
} | |
public function getName() | |
{ | |
return 'comment'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment