概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:38:18 | 113 次阅读 | 评论: 0 | 来源: 网络整理

Validates that a value is of a specific data type. For example, if a variable should be an array, you can use this constraint with the array type option to validate this.

Applies to property or method
Options
Class Type
Validator TypeValidator

Basic Usage

  • YAML
    # src/Acme/BlogBundle/Resources/config/validation.yml
    AcmeBlogBundleEntityAuthor:
        properties:
            age:
                - Type:
                    type: integer
                    message: The value {{ value }} is not a valid {{ type }}.
    
  • Annotations
    // src/Acme/BlogBundle/Entity/Author.php
    namespace AcmeBlogBundleEntity;
    
    use SymfonyComponentValidatorConstraints as Assert;
    
    class Author
    {
        /**
         * @AssertType(type="integer", message="The value {{ value }} is not a valid {{ type }}.")
         */
        protected $age;
    }
    
  • XML
    <!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
    
        <class name="AcmeBlogBundleEntityAuthor">
            <property name="age">
                <constraint name="Type">
                    <option name="type">integer</option>
                    <option name="message">The value {{ value }} is not a valid {{ type }}.</option>
                </constraint>
            </property>
        </class>
    </constraint-mapping>
    
  • PHP
    // src/Acme/BlogBundle/Entity/Author.php
    namespace AcmeBlogBundleEntity;
    
    use SymfonyComponentValidatorMappingClassMetadata;
    use SymfonyComponentValidatorConstraints as Assert;
    
    class Author
    {
        public static function loadValidatorMetadata(ClassMetadata $metadata)
        {
            $metadata->addPropertyConstraint('age', new AssertType(array(
                'type'    => 'integer',
                'message' => 'The value {{ value }} is not a valid {{ type }}.',
            )));
        }
    }
    

Options

type

type: string [default option]

This required option is the fully qualified class name or one of the PHP datatypes as determined by PHP’s is_ functions.

Also, you can use ctype_ functions from corresponding built-in PHP extension. Consider a list of ctype functions:

Make sure that the proper locale is set before using one of these.

message

type: string default: This value should be of type {{ type }}.

The message if the underlying data is not of the given type.

payload

type: mixed default: null

2.6 新版功能: The payload option was introduced in Symfony 2.6.

This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to.

For example, you may want to used several error levels to present failed constraint differently in the front-end depending on the severity of the error.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务