@@ -102,51 +102,70 @@ const CreateAccount = ({
102
102
setAccounts,
103
103
} ) => {
104
104
const [ fullName , setFullName ] = useState ( accounts . fullName ) ;
105
- const [ _userName , _setUserName ] = useState ( accounts . userName ) ;
105
+ const [ checkFullName , setCheckFullName ] = useState ( accounts . fullName ) ;
106
+ const [ fullNameInvalidHint , setFullNameInvalidHint ] = useState ( "" ) ;
107
+ const [ isFullNameValid , setIsFullNameValid ] = useState ( null ) ;
106
108
const [ userName , setUserName ] = useState ( accounts . userName ) ;
109
+ const [ checkUserName , setCheckUserName ] = useState ( accounts . userName ) ;
107
110
const [ userNameInvalidHint , setUserNameInvalidHint ] = useState ( "" ) ;
108
111
const [ isUserNameValid , setIsUserNameValid ] = useState ( null ) ;
109
112
const [ password , setPassword ] = useState ( accounts . password ) ;
110
113
const [ confirmPassword , setConfirmPassword ] = useState ( accounts . confirmPassword ) ;
111
114
const [ isPasswordValid , setIsPasswordValid ] = useState ( false ) ;
112
115
113
116
useEffect ( ( ) => {
114
- debounce ( 300 , ( ) => setUserName ( _userName ) ) ( ) ;
115
- } , [ _userName , setUserName ] ) ;
117
+ debounce ( 300 , ( ) => setCheckUserName ( userName ) ) ( ) ;
118
+ } , [ userName , setCheckUserName ] ) ;
116
119
117
120
useEffect ( ( ) => {
118
- setIsUserValid ( isPasswordValid && isUserNameValid ) ;
119
- } , [ setIsUserValid , isPasswordValid , isUserNameValid ] ) ;
121
+ debounce ( 300 , ( ) => setCheckFullName ( fullName ) ) ( ) ;
122
+ } , [ fullName , setCheckFullName ] ) ;
123
+
124
+ useEffect ( ( ) => {
125
+ setIsUserValid ( isPasswordValid && isUserNameValid && isFullNameValid !== false ) ;
126
+ } , [ setIsUserValid , isPasswordValid , isUserNameValid , isFullNameValid ] ) ;
120
127
121
128
useEffect ( ( ) => {
122
129
let valid = true ;
123
130
setUserNameInvalidHint ( "" ) ;
124
- if ( userName . length === 0 ) {
131
+ if ( checkUserName . length === 0 ) {
125
132
valid = null ;
126
- } else if ( userName . length > 32 ) {
133
+ } else if ( checkUserName . length > 32 ) {
127
134
valid = false ;
128
135
setUserNameInvalidHint ( _ ( "User names must be shorter than 33 characters" ) ) ;
129
- } else if ( reservedNames . includes ( userName ) ) {
136
+ } else if ( reservedNames . includes ( checkUserName ) ) {
130
137
valid = false ;
131
138
setUserNameInvalidHint ( _ ( "User name must not be a reserved word" ) ) ;
132
- } else if ( isUserNameWithInvalidCharacters ( userName ) ) {
139
+ } else if ( isUserNameWithInvalidCharacters ( checkUserName ) ) {
133
140
valid = false ;
134
141
setUserNameInvalidHint ( cockpit . format ( _ ( "User name may only contain: letters from a-z, digits 0-9, dash $0, period $1, underscore $2" ) , "-" , "." , "_" ) ) ;
135
142
}
136
143
setIsUserNameValid ( valid ) ;
137
- } , [ userName ] ) ;
144
+ } , [ checkUserName ] ) ;
145
+
146
+ useEffect ( ( ) => {
147
+ let valid = true ;
148
+ setFullNameInvalidHint ( "" ) ;
149
+ if ( checkFullName . length === 0 ) {
150
+ valid = null ;
151
+ } else if ( ! checkFullName . match ( / ^ [ ^ : ] * $ / ) ) {
152
+ valid = false ;
153
+ setFullNameInvalidHint ( _ ( "Full name cannot contain colon characters" ) ) ;
154
+ }
155
+ setIsFullNameValid ( valid ) ;
156
+ } , [ checkFullName ] ) ;
138
157
139
158
const passphraseForm = (
140
159
< PasswordFormFields
141
160
idPrefix = { idPrefix }
142
161
policy = { passwordPolicy }
143
- initialPassword = { password }
162
+ password = { password }
163
+ setPassword = { setPassword }
144
164
passwordLabel = { _ ( "Passphrase" ) }
145
- initialConfirmPassword = { confirmPassword }
165
+ confirmPassword = { confirmPassword }
166
+ setConfirmPassword = { setConfirmPassword }
146
167
confirmPasswordLabel = { _ ( "Confirm passphrase" ) }
147
168
rules = { [ ruleLength ] }
148
- onChange = { setPassword }
149
- onConfirmChange = { setConfirmPassword }
150
169
setIsValid = { setIsPasswordValid }
151
170
/>
152
171
) ;
@@ -155,7 +174,9 @@ const CreateAccount = ({
155
174
setAccounts ( ac => ( { ...ac , fullName, userName, password, confirmPassword } ) ) ;
156
175
} , [ setAccounts , fullName , userName , password , confirmPassword ] ) ;
157
176
158
- const userNameValidated = isUserNameValid === null ? "default" : isUserNameValid ? "success" : "error" ;
177
+ const getValidatedVariant = ( valid ) => valid === null ? "default" : valid ? "success" : "error" ;
178
+ const userNameValidated = getValidatedVariant ( isUserNameValid ) ;
179
+ const fullNameValidated = getValidatedVariant ( isFullNameValid ) ;
159
180
160
181
return (
161
182
< Form
@@ -177,7 +198,16 @@ const CreateAccount = ({
177
198
id = { idPrefix + "-full-name" }
178
199
value = { fullName }
179
200
onChange = { ( _event , val ) => setFullName ( val ) }
201
+ validated = { fullNameValidated }
180
202
/>
203
+ { fullNameValidated === "error" &&
204
+ < FormHelperText >
205
+ < HelperText >
206
+ < HelperTextItem variant = { fullNameValidated } >
207
+ { fullNameInvalidHint }
208
+ </ HelperTextItem >
209
+ </ HelperText >
210
+ </ FormHelperText > }
181
211
</ FormGroup >
182
212
< FormGroup
183
213
label = { _ ( "User name" ) }
@@ -186,8 +216,8 @@ const CreateAccount = ({
186
216
< InputGroup id = { idPrefix + "-user-name-input-group" } >
187
217
< TextInput
188
218
id = { idPrefix + "-user-name" }
189
- value = { _userName }
190
- onChange = { ( _event , val ) => _setUserName ( val ) }
219
+ value = { userName }
220
+ onChange = { ( _event , val ) => setUserName ( val ) }
191
221
validated = { userNameValidated }
192
222
/>
193
223
</ InputGroup >
0 commit comments