CssColor

Validates that a value is a valid CSS color. The underlying value is casted to a string before being validated.

値が有効な CSS カラーであることを検証します。基になる値は、検証される前に文字列にキャストされます。
Applies to property or method
Class CssColor
Validator CssColorValidator

Basic Usage

In the following example, the $defaultColor value must be a CSS color defined in any of the valid CSS formats (e.g. red, #369, hsla(0, 0%, 20%, 0.4)); the $accentColor must be a CSS color defined in hexadecimal format; and $currentColor must be a CSS color defined as any of the named CSS colors:

次の例では、$defaultColor 値は、有効な CSS 形式のいずれかで定義された CSS カラーでなければなりません (例: red, #369,hsla(0, 0%, 20%, 0.4)); $accentColor は、16 進数形式で定義された CSS カラーでなければなりません。 $currentColor は、指定された CSS カラーのいずれかとして定義された CSS カラーである必要があります。
  • Attributes
    属性
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// src/Entity/Bulb.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Bulb
{
    #[Assert\CssColor]
    protected $defaultColor;

    #[Assert\CssColor(
        formats: Assert\CssColor::HEX_LONG,
        message: 'The accent color must be a 6-character hexadecimal color.',
    )]
    protected $accentColor;

    #[Assert\CssColor(
        formats: [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS],
        message: 'The color '{{ value }}' is not a valid CSS color name.',
    )]
    protected $currentColor;
}

Note

ノート

As with most of the other constraints, null and empty strings are considered valid values. This is to allow them to be optional values. If the value is mandatory, a common solution is to combine this constraint with NotBlank.

他のほとんどの制約と同様に、null および空の文字列は有効な値と見なされます。これは、それらをオプションの値にできるようにするためです。値が必須の場合、一般的な解決策は、この制約と NotBlank を組み合わせることです。

Options

groups

type: array | string

タイプ: 配列 |ストリング

It defines the validation group or groups of this constraint. Read more about validation groups.

この制約の検証グループを定義します。検証グループの詳細を参照してください。

message

type: string default: This value is not a valid CSS color.

タイプ: 文字列 デフォルト: この値は有効な CSS カラーではありません。

This message is shown if the underlying data is not a valid CSS color.

このメッセージは、基になるデータが有効な CSS カラーでない場合に表示されます。

You can use the following parameters in this message:

このメッセージでは、次のパラメーターを使用できます。
Parameter Description
{{ value }} The current (invalid) value

formats

type: string | array

タイプ: 文字列 |配列

By default, this constraint considers valid any of the many ways of defining CSS colors. Use the formats option to restrict which CSS formats are allowed. These are the available formats (which are also defined as PHP constants; e.g. Assert\CssColor::HEX_LONG):

デフォルトでは、この制約は、CSS カラーを定義する多くの方法のいずれかが有効であると見なします。使用できる CSS 形式を制限するには、formats オプションを使用します。これらは、使用可能な形式です (PHP 定数としても定義されています。例: Assert\CssColor::HEX_LONG):
  • hex_long
    hex_long
  • hex_long_with_alpha
    hex_long_with_alpha
  • hex_short
    hex_short
  • hex_short_with_alpha
    hex_short_with_alpha
  • basic_named_colors
    basic_named_colors
  • extended_named_colors
    extended_named_colors
  • system_colors
    system_colors
  • keywords
    キーワード
  • rgb
    RGB
  • rgba
    RGBA
  • hsl
    hsl
  • hsla
    hsla

hex_long

A regular expression. Allows all values which represent a CSS color of 6 characters (in addition of the leading #) and contained in ranges: 0 to 9 and A to F (case insensitive).

正規表現。 6 文字 (先頭の # に加えて) の CSS カラーを表し、0 から 9 および A から F (大文字と小文字を区別しない) の範囲に含まれるすべての値を許可します。

Examples: #2F2F2F, #2f2f2f

例: #2F2F2F、#2f2f2f

hex_long_with_alpha

A regular expression. Allows all values which represent a CSS color with alpha part of 8 characters (in addition of the leading #) and contained in ranges: 0 to 9 and A to F (case insensitive).

正規表現。 8 文字の alphapart (先頭の # に加えて) を持ち、0 から 9 および A から F (大文字と小文字を区別しない) の範囲に含まれる CSS カラーを表すすべての値を許可します。

Examples: #2F2F2F80, #2f2f2f80

例: #2F2F2F80、#2f2f2f80

hex_short

A regular expression. Allows all values which represent a CSS color of strictly 3 characters (in addition of the leading #) and contained in ranges: 0 to 9 and A to F (case insensitive).

正規表現。厳密に 3 文字 (先頭の # に加えて) の CSS カラーを表し、0 から 9 および A から F (大文字と小文字を区別しない) の範囲に含まれるすべての値を許可します。

Examples: #CCC, #ccc

例: #CCC、#ccc

hex_short_with_alpha

A regular expression. Allows all values which represent a CSS color with alpha part of strictly 4 characters (in addition of the leading #) and contained in ranges: 0 to 9 and A to F (case insensitive).

正規表現。厳密に 4 文字 (先頭の # を追加) の alphapart で CSS カラーを表すすべての値を許可し、0 から 9 および A から F (大文字と小文字を区別しない) の範囲を含みます。

Examples: #CCC8, #ccc8

例: #CCC8、#ccc8

basic_named_colors

Any of the valid color names defined in the W3C list of basic named colors (case insensitive).

基本的な名前付きの色の W3C リストで定義されている有効な色名のいずれか (大文字と小文字は区別されません)。

Examples: black, red, green

例: 黒、赤、緑

extended_named_colors

Any of the valid color names defined in the W3C list of extended named colors (case insensitive).

拡張名前付き色の W3C リストで定義されている有効な色名のいずれか (大文字と小文字を区別しない)。

Examples: aqua, brown, chocolate

例: アクア、ブラウン、チョコレート

system_colors

Any of the valid color names defined in the CSS WG list of system colors (case insensitive).

システム カラーの CSS WG リストで定義されている有効なカラー名のいずれか (大文字と小文字を区別しない)。

Examples: LinkText, VisitedText, ActiveText, ButtonFace, ButtonText

例: LinkText、VisitedText、ActiveText、ButtonFace、ButtonText

keywords

Any of the valid keywords defined in the CSS WG list of keywords (case insensitive).

CSS WG のキーワード リストで定義されている有効なキーワードのいずれか (大文字と小文字を区別しない)。

Examples: transparent, currentColor

例: 透明、currentColor

rgb

A regular expression. Allows all values which represent a CSS color following the RGB notation, with or without space between values.

正規表現。値間のスペースの有無にかかわらず、RGB 表記に従う CSS カラーを表すすべての値を許可します。

Examples: rgb(255, 255, 255), rgb(255,255,255)

例: rgb(255, 255, 255)、rgb(255,255,255)

rgba

A regular expression. Allows all values which represent a CSS color with alpha part following the RGB notation, with or without space between values.

正規表現。値間のスペースの有無にかかわらず、RGB 表記に従う alphapart を持つ CSS カラーを表すすべての値を許可します。

Examples: rgba(255, 255, 255, 0.3), rgba(255,255,255,0.3)

例: rgba(255, 255, 255, 0.3)、rgba(255,255,255,0.3)

hsl

A regular expression. Allows all values which represent a CSS color following the HSL notation, with or without space between values.

正規表現。値間のスペースの有無にかかわらず、HSL 表記に従う CSS カラーを表すすべての値を許可します。

Examples: hsl(0, 0%, 20%), hsl(0,0%,20%)

例: hsl(0, 0%, 20%), hsl(0,0%,20%)

hsla

A regular expression. Allows all values which represent a CSS color with alpha part following the HSLA notation, with or without space between values.

正規表現。値間のスペースの有無にかかわらず、HSLA 表記に従う alphapart を持つ CSS カラーを表すすべての値を許可します。

Examples: hsla(0, 0%, 20%, 0.4), hsla(0,0%,20%,0.4)

例: hsla(0, 0%, 20%, 0.4), hsla(0,0%,20%,0.4)

payload

type: mixed default: null

タイプ: 混合 デフォルト: null

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 you.

このオプションは、任意のドメイン固有のデータを制約に添付するために使用できます。構成されたペイロードは Validator コンポーネントによって使用されませんが、その処理は完全にユーザー次第です。

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

たとえば、いくつかのエラー レベルを使用して、エラーの重大度に応じて、失敗した制約をフロントエンドで異なる方法で提示することができます。