PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – YML验证文件被忽略大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是Symfony 2 Web框架的新功能,并且正在努力进行非常基本的验证任务.我有一个实体模型Post有一个成员slug,我用来构建链接的帖子.在Post.orm.yml中,我定义了unique:true,并希望将此约束作为验证器.

我已经创建了一个文件validation.yml:

# src/OwnBundles/BlogpostBundle/resources/config/validation.yml

OwnBundles\BlogpostBundle\Entity\Post:
    properties:
        slug:
            - NotBlank: ~
    consTraints:
        - Symfony\Bridge\Doctrine\Validator\ConsTraints\UniqueEntity: slug

我的控制器中的创建功能非常简单:

public function addAction(request $request)
{
    $post = new Post();
    $form = $this->createForm(new PostType(),$post);

    if($request->getmethod() == 'POST')
    {
        $form->bind($request);
        if($form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($post);
            $em->flush();
            return $this->redirect(
                $this->generateUrl('own_bundles_blogpost_homepage')
            );
        }
    }
    return $this->render(
        'OwnBundlesBlogpostBundle:Default:add.html.twig',array(
            'title' => 'Add new blogpost','form' => $form->createView(),)
    );
}

基本的页面流工作正常,我可以添加帖子并看到它们,但是如果我复制一个标题来测试我的验证,它会抛出一个异常:sqlSTATE [23000]:完整性约束违规:1062重复条目“重复插入”键’UNIQ_FAB8C3B3989D9B62′.我一直在扫描文档已经有一段时间了,但是我无法找出为什么我的$form-> isValid()返回true.

您是否在app / config / config.yml中启用验证?
...

framework:
    ...
    validation:    { enabled: true }
    ...

...

并且如果要使用注释定义验证,则必须同时启用验证和注释验证:

...

framework:
    ...
    validation:    { enabled: true,enable_Annotations: true }
    ...

...

然后不要忘记清除app / cache目录.

大佬总结

以上是大佬教程为你收集整理的php – YML验证文件被忽略全部内容,希望文章能够帮你解决php – YML验证文件被忽略所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。