Symfony,从提交的表格中生成slu ..

时间:2017-11-05 14:09:28

标签: php forms symfony security slugify

我有这个表单,我想修改它以添加一个应该从用户提交的数据生成的slug字段。

与用户选择的类别(名称)和内容中的随机字词类似,我也使用Slugify来清理网址:

有什么想法吗?

 $u = $this->getUser();
 $sl = new Slugify();
 $task = new AdsList();
 $task->setPostXpr(new \DateTime('+1 month'));
 $task->setSlug($ans);
 $task->setPostedBy($u);
 $form = $this->createFormBuilder($task)
     ->add('title', TextType::class)
     ->add('content', TextareaType::class)
     ->add('category', EntityType::class, array(
         // query choices from CategoryAd.Name
         'class' => 'AppBundle:CategoryAd',
         'choice_label' => 'name',
     ))
     ->add('postXpr', DateType::class, array(
         'widget' => 'single_text',
         // this is actually the default format for single_text
         'format' => 'yyyy-MM-dd',
         'label' => 'Post Expire',
     ))
     ->add('save', SubmitType::class, array('label' => 'Create Post'))
        ->getForm();

1 个答案:

答案 0 :(得分:0)

您只需要将映射信息添加到slug字段的实体,但不要添加到表单,然后使用cocur / slugify

  

使用cocur

if ($form->isSubmitted() && $form->isValid()) {
    $em = $this->getDoctrine()->getManager();
    $entity->setSlug($this->get('cocur_slugify')->slugify($entity->getName()));
    $em->persist($entity);
    $em->flush();
    return $this->redirectToRoute('my_route_to_redirect', array('slug' => $entity->getSlug()));
        }

或者您可以将学说扩展用作@Alexander Br。贴