首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Nelmio ApiDocBundle中指定参数的格式

如何在Nelmio ApiDocBundle中指定参数的格式
EN

Stack Overflow用户
提问于 2015-10-19 19:10:53
回答 3查看 9.2K关注 0票数 7

我使用@ApiDoc注释的@ApiDoc属性来指定属于表单字段的api参数。

代码语言:javascript
运行
复制
 * @ApiDoc(
 *      section="User",
 *      resource=true,
 *      input={
 *          "class"="Nik\UserBundle\Form\UserType",
 *      },
 *     ....

表单的data_class是一个对属性具有约束验证的实体。

我希望nelmio文档指定参数格式作为实体的验证约束,但是格式是空的。

如何在nelmio ApiDocBundle中指定参数格式?

编辑:也许我写了一个糟糕的问题。

我们可以为inputoutput指定解析器,如果没有为这些解析器指定解析器,则调用inputoutput的所有解析器,然后调用UserType的所有解析器。

nelmio有一个名为ValidationParser的解析器,它有一个名为parseConstraint的方法,该方法将format设置为输入和输出,但是这个方法不是为我的文档调用的,为什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-24 12:38:52

我提交一个对format属性使用验证元数据的拉请求。

你可以看到这个PR 这里

票数 2
EN

Stack Overflow用户

发布于 2015-10-27 09:32:54

格式列仅为datetimedatechoice类型设计。

对于datetimedate,它表示日期格式(如Y-m-d H:i:s )和用于choice的选项数组。

我还没有找到任何关于它的文档,所以我不得不查看源代码。这是FormTypeParser类,实际上是解析FormType的地方,您可以看到格式化字段是如何设置的。

FormTypeParserTest类中,您可以看到如何使用它。只需为其中一个可用类型传递具有format名称的字符串参数,解析器就会处理它。

更新:要在FormType类中定义约束的

例如:

代码语言:javascript
运行
复制
class TestType extends AbstractType
{
    /**
     * @Assert\Type("string")
     * @Assert\Length(min="10", max="255")
     * @Assert\Regex("/^[^<>]+$/i")
     */
    private $title;

    /**
     * @Assert\Type("string")
     * @Assert\Length(min="10", max="255")
     * @Assert\Regex("/^[^<>]+$/i")
     */
    private $content;

    /**
     * @Assert\Date()
     */
    private $created;

    public function getName()
    {
        return 'test';
    }
}

将被分析为:

ValidationParserdoParse()方法中查找在FormType类中定义的所有约束,然后对每个约束执行parseConstraint()方法。

您还可以像我前面所描述的那样使用FormTypeParser。例如:

代码语言:javascript
运行
复制
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('created', 'date', array('label' => 'Created', 'format' => 'yyyy-MM-dd'))
        ->add('color', 'choice', array('label' => 'Color', 'choices' => array('grey' => '#CCCCCC', 'red' => '#FF0000')))
        ->add('save', 'submit');
}

将被解析为:

希望它能帮上忙!

票数 6
EN

Stack Overflow用户

发布于 2015-10-26 09:50:25

如您所见,这里可以在注释中指定过滤器,就像它所做的文档示例一样。

这里是这个例子的一部分:

代码语言:javascript
运行
复制
/**
 * This is the documentation description of your method, it will appear
 * on a specific pane. It will read all the text until the first
 * annotation.
 *
 * @ApiDoc(
 *  resource=true,
 *  description="This is a description of your API method",
 *  filters={
 *      {"name"="a-filter", "dataType"="integer"},
 *      {"name"="another-filter", "dataType"="string", "pattern"="(foo|bar) ASC|DESC"}
 *  }
 * )
 */
public function getAction()
{
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33222064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档