Help: I want to register at the time, add a judgment, the user name is unique, this how to do?
This issue has been closed because of a duplicate of an existing problem
Reply content:
Help: I want to register at the time, add a judgment, the user name is unique, this how to do?
Call the database Judge Yes
Listen to the user name form input events, KeyUp or blur, the event triggered by using AJAX to submit the user name to the server to make judgments, the results returned, the front-end to do the corresponding processing.
It is unclear if you want to use Ajax, if you are simply validating or constraining from symfony, you need to constrain the user name (assuming the user name is username) as the following constraint (@ORM \column plus unique=true constraint) when defining the entity. At the same time entity header plus @uniqueentity (fields= "username") constraint), after submission, Symfony automatically verifies that username is unique, and if the validation fails, the error message (message) is indicated:
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;/** *@ORM\Entity *@UniqueEntity(fields="username", message="用户名已存在") */class User{ /** *@ORM\Column(type="string", length=255, unique=true) *@ORM\NotBlank() */ protected $username; //...}
Note:
@ORM \column (unique=true) is reflected on specific SQL statements (unique KEY) and is a database-level constraint
@UniqueEntity is part of Symfony validation (Validator) and is a constraint (validation) at the Symfony business logic level