2017-01-29 86 views
1

我有這樣的代碼在實體:symfony的斷言忽略自定義消息

/** 
    * @Assert\Type(
    *  type="real", 
    *  message="Price for vehicle service must be real number." 
    *) 
    * 
    * @ORM\Column(name="min_price", type="float") 
    */ 
    protected $minPrice; 

但是,如果以這種形式字段中輸入非整數值,Symfony的返回(在我的情況Значениенедопустимо)默認語言環境的錯誤信息,而不是「車輛服務價格必須是實數。」這是不舒服的,因爲在錯誤中沒有指定字段名稱,並且如果存在複雜形式,需要時間來查找該字段。

也許作爲解決方案可以回調驗證,但在我的主觀意見,它不會出現,因爲用戶只能看到區域默認消息。當我試圖避免添加@Assert \ Regex(pattern =「/ + d /」,message =「custom message」)時,我無論如何都看到了本地默認消息。

Symfony的探查顯示如下錯誤:

Symfony\Component\Validator\ConstraintViolation 
Object(Symfony\Component\Form\Form).children[vehiclePriceOutOfTown].children[minPrice] = g 
Caused by: 
Symfony\Component\Form\Exception\TransformationFailedException 
Unable to reverse value for property path "minPrice": Number parsing failed: U_PARSE_ERROR 
Caused by: 
Symfony\Component\Form\Exception\TransformationFailedException 
Number parsing failed: U_PARSE_ERROR 

回答

0

看到用戶輸入的轉型過程中引發的異常。這發生在實際驗證邏輯應用之前(這發生在POST_SUBMIT事件的一部分)。如果您想要自定義在引發TransformationFailedException時顯示的錯誤消息,則可以將invalid_message option用於特定的表單類型。

+0

謝謝你,只要加上'invalid_message'就像你說的解決了這個問題。 –