@@ -21,7 +21,9 @@ class UniqueTranslationValidator
21
21
*/
22
22
public function validate ($ attribute , $ value , $ parameters , $ validator )
23
23
{
24
- list ($ name , $ locale ) = $ this ->getAttributeNameAndLocale ($ attribute );
24
+ list ($ name , $ locale ) = $ this ->isNovaTranslation ($ attribute )
25
+ ? $ this ->getNovaAttributeNameAndLocale ($ attribute )
26
+ : $ this ->getArrayAttributeNameAndLocale ($ attribute );
25
27
26
28
if ($ this ->isUnique ($ value , $ name , $ locale , $ parameters )) {
27
29
return true ;
@@ -49,6 +51,7 @@ protected function setMissingErrorMessages($validator, $name, $locale)
49
51
"{$ name }. {$ rule }" ,
50
52
"{$ name }.*. {$ rule }" ,
51
53
"{$ name }. {$ locale }. {$ rule }" ,
54
+ "translations_ {$ name }_ {$ locale }. {$ rule }" ,
52
55
];
53
56
54
57
foreach ($ keys as $ key ) {
@@ -58,21 +61,87 @@ protected function setMissingErrorMessages($validator, $name, $locale)
58
61
}
59
62
}
60
63
64
+ /**
65
+ * Check if the attribute is a Nova translation field name.
66
+ *
67
+ * @param string $attribute
68
+ *
69
+ * @return bool
70
+ */
71
+ protected function isNovaTranslation ($ attribute )
72
+ {
73
+ return strpos ($ attribute , '. ' ) === false && strpos ($ attribute , 'translations_ ' ) === 0 ;
74
+ }
75
+
76
+ /**
77
+ * Get the attribute name and locale of a Nova translation field.
78
+ *
79
+ * @param string $attribute
80
+ *
81
+ * @return array
82
+ */
83
+ protected function getNovaAttributeNameAndLocale ($ attribute )
84
+ {
85
+ $ attribute = str_replace ('translations_ ' , '' , $ attribute );
86
+
87
+ return $ this ->getAttributeNameAndLocale ($ attribute , '_ ' );
88
+ }
89
+
90
+ /**
91
+ * Get the attribute name and locale of an array field.
92
+ *
93
+ * @param string $attribute
94
+ *
95
+ * @return array
96
+ */
97
+ protected function getArrayAttributeNameAndLocale ($ attribute )
98
+ {
99
+ return $ this ->getAttributeNameAndLocale ($ attribute , '. ' );
100
+ }
101
+
61
102
/**
62
103
* Get the attribute name and locale.
63
104
*
64
105
* @param string $attribute
106
+ * @param string $delimiter
65
107
*
66
108
* @return array
67
109
*/
68
- protected function getAttributeNameAndLocale ($ attribute )
110
+ protected function getAttributeNameAndLocale ($ attribute , $ delimiter )
111
+ {
112
+ $ locale = $ this ->getAttributeLocale ($ attribute , $ delimiter );
113
+ $ name = $ this ->getAttributeName ($ attribute , $ locale , $ delimiter );
114
+
115
+ return [$ name , $ locale ?: App::getLocale ()];
116
+ }
117
+
118
+ /**
119
+ * Get the locale from the attribute name.
120
+ *
121
+ * @param string $attribute
122
+ * @param string $delimiter
123
+ *
124
+ * @return string|null
125
+ */
126
+ protected function getAttributeLocale ($ attribute , $ delimiter )
69
127
{
70
- $ parts = explode ( ' . ' , $ attribute );
128
+ $ pos = strrpos ( $ attribute , $ delimiter );
71
129
72
- $ name = $ parts [ 0 ] ;
73
- $ locale = $ parts [ 1 ] ?? App:: getLocale ();
130
+ return $ pos > 0 ? substr ( $ attribute , $ pos + 1 ) : null ;
131
+ }
74
132
75
- return [$ name , $ locale ];
133
+ /**
134
+ * Get the attribute name without the locale.
135
+ *
136
+ * @param string $attribute
137
+ * @param string|null $locale
138
+ * @param string $delimiter
139
+ *
140
+ * @return string
141
+ */
142
+ protected function getAttributeName ($ attribute , $ locale , $ delimiter )
143
+ {
144
+ return $ locale ? str_replace ("{$ delimiter }{$ locale }" , '' , $ attribute ) : $ attribute ;
76
145
}
77
146
78
147
/**
0 commit comments