WordCount
7.2
WordCount 約束條件在 Symfony 7.2 中引入。
驗證字串(或實作 Stringable PHP 介面的物件)包含指定的字數。在內部,此約束條件使用 IntlBreakIterator 類別根據您的語系計算字數。
適用於 | 屬性或方法 |
類別 | WordCount |
驗證器 | WordCountValidator |
基本用法
如果您想確保 BlogPostDTO 類別的 content 屬性包含介於 100 到 200 字之間的字數,您可以執行以下操作
1 2 3 4 5 6 7 8 9 10
// src/Entity/BlogPostDTO.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class BlogPostDTO
{
#[Assert\WordCount(min: 100, max: 200)]
protected string $content;
}
選項
最小值訊息
類型: string
預設值: This value is too short. It should contain at least one word.|This value is too short. It should contain at least {{ min }} words.
如果數值未包含至少最少字數,將會顯示此訊息。
您可以在此訊息中使用以下參數
參數 | 描述 |
---|---|
{{ min }} |
最少字數 |
{{ count }} |
實際字數 |
最大值訊息
類型: string
預設值: This value is too long. It should contain one word.|This value is too long. It should contain {{ max }} words or less.
如果數值包含超過最多字數,將會顯示此訊息。
您可以在此訊息中使用以下參數
參數 | 描述 |
---|---|
{{ max }} |
最多字數 |
{{ count }} |
實際字數 |
本作品,包括程式碼範例,依Creative Commons BY-SA 3.0 授權條款授權。