Symfony registration page to determine the user name unique

Source: Internet
Author: User
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:

    1. @ORM \column (unique=true) is reflected on specific SQL statements (unique KEY) and is a database-level constraint

    2. @UniqueEntity is part of Symfony validation (Validator) and is a constraint (validation) at the Symfony business logic level

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.