Question
In the quick registration form, you need to check if the login already exists. If it does, do not allow the form to be saved.
Answer
To create a registration form using Quick Form with login control, you need to perform the following actions:
Create a simple table to receive applications for user creation. We create a separate table because direct form access to the Users table is an unsafe approach.
In the settings of the field where the user specifies the desired login, you need to add regexp
(Input verification format), similar to the login
field in the users
table: ^[a-z0-9_\-.]{3,30}$
.
In the quick form, the Save button does not execute code actions by itself — it saves the row in the table. Therefore, you need to add a code action with an On Add trigger to any field, which will create a row in the users table. Usually, the first field of the row part is used for such purposes.
In the code action that adds the user, the first action is to take select
by the new login from the users
table, and if it is not empty (login exists), then issue errorException
— this will stop all subsequent actions. If the login does not exist, then the second action is to add the user.
Example of code action on adding in the first field in the applications table:
a1=: if(condition: $loginexist != ""; then: $err)
loginexist: select(table: 'users'; field: 'id'; where: 'login' = #login)
err: errorException(text: "Login already in use, choose another!")
a2=: insert(table: 'users'; field: 'login' = #login; field: 'pass' = #password; field: 'email' = #email; log: true)
As an improvement to the user experience, you can check the availability of the login in the field formatting code #login
and inform the user that the login they entered already exists before pressing the Save button.
Example of field formatting code for login in the applications table:
f1=: setFormat(condition: $#nfv != ""; condition: $loginexist != ""; text: str`"Login" ++ $#nfv ++ "<b><span style='color:red'>already in use!</span></b>"`)
loginexist: select(table: 'users'; field: 'id'; where: 'login' = $#nfv)