我想在表单错误消息中添加href链接,并且此链接不是静态的

时间:2017-04-28 05:21:33

标签: php symfony

我想在表单错误消息中添加href链接,这个链接不是静态的。我试试更多的方式,但没有相关的方法。我需要帮助

index.html.twig {% extends ... %} {% form_theme form 'Form/bootstrap_3_horizontal_errors_layout.html.twig' %}

bootstrap_3_horizo​​ntal_errors_layout.html.twig

 {% extends 'bootstrap_3_horizontal_layout.html.twig' %}
    {% block form_errors -%}
        {% if errors|length > 0 -%}
        {% if form.parent %}<span class="help-block">{% else %}<div class="alert alert-danger">{% endif %}
        <ul class="list-unstyled">
            {%- for error in errors -%}
                <li>
                    <span class="glyphicon glyphicon-exclamation-sign"></span> {{ error.message }}
                    <a href="{{ route }}">delete</a>
                </li>
            {%- endfor -%}
        </ul>
        {% if form.parent %}</span>{% else %}</div>{% endif %}
        {%- endif %}
    {%- endblock form_errors %}

validator.php

public function validate($cooperation, Constraint $constraint)
{
    $couponCodeOr400 = $cooperation->getCouponCodeOr400();

    $qb = $this->repo->createQueryBuilder('c')
        ->select('c.id')
        ->where('c.couponCodeOr400 = :couponCodeOr400')
        ->setParameters(compact('couponCodeOr400'));

    $id = $cooperation->getId();
    if (null !== $id) {
        $qb->andWhere('c.id != :id')->setParameter('id', $id);
    }

    $alreadyId = $qb->getQuery()->getOneOrNullResult();

    if ($alreadyId) {
        $this->context->buildViolation(sprintf('coupon %s is already exist', $couponCodeOr400))
            ->addViolation();
    }
}

在控制器中,我需要这个ID为&#34; route&#34; twig上的参数,如何传回这个值。

控制器

public function newAction(Request $request)
{
    $form = $this->createForm(CooperationType::class);

    $form->handleRequest($request);
    if ($form->isValid()) {
        $cooperation = $form->getData();

        $em = $this->get('doctrine.orm.entity_manager');
        $em->persist($cooperation);
        $em->flush();

        return $this->redirectToRoute('admin_cooperation_edit', ['id' => $cooperation->getId()]);
    }

    return $this->render('admin/cooperation/new.html.twig', [
        'form' => $form->createView(),
    ]);
}

希望您有更好的方式在表单错误中添加链接

0 个答案:

没有答案