Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9113607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:00:13+00:00 2026-06-17T04:00:13+00:00

I’ve been looking at my options for Symfony2 validation. The problem is this: I

  • 0

I’ve been looking at my options for Symfony2 validation. The problem is this:

I have a Member class that hashes a password in the setPassword method:

public function setPassword($password)
{
    $this->password = Encrypt::cryptPassword($password);
}

The hash is a long string that exists regardless of the original input.

For this reason validation like this is useless:

 * @Assert\NotBlank()
 * @Assert\Length(
 *      min = "8",
 *      max = "15",
 *      minMessage = "Your password must be at least {{ limit }} characters",
 *      maxMessage = "Your password cannot be longer than {{ limit }} characters"
 * ) 

The validation is triggered because the hash string is created regardless of the validity of the input.

I looked at trying a getter validation, but this also seems to be a dead end, because again, what I really want to validate is the original input to setPassword() and getting the hash value after setPassword has created a hash value isn’t helpful.

What’s the recommended approach to this that works with the existing Validation component that will let me validate the original user input?

Update

Although I accepted the answer, it is not really viable. It did cause
me to focus in on the lifecycle callback system which will probably be
part of the solution.

Given the current suggestion, once the password is encoded the first
time, validation is exceeded anytime you try and edit the entity
because the hashed password is of course not the input and cannot be
decrypted into the original input.

I’m experimenting now with a separate attribute in the entity, purely
for the entry of the password, along with the use of validation groups
to trigger the use of validation in certain circumstances (new user
creation/ password change) while omitting it in other cases. I do
plan to make use of the prePersist and preUpdate callbacks to do the
encoding of the password.

Just for the record, this is being employed in an application that
provides a REST api, as well as administration tools and a web front
end for selected functionality.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:00:14+00:00Added an answer on June 17, 2026 at 4:00 am

    Use doctrine lifecycles and never modify the behavior of a setter !

    <?php
    
    // ...
    
    /**
     * @ORM\Entity()
     * @ORM\HasLifecycleCallbacks()
     */
    class Member
    {
        public function setPassword($password)
        {
            $this->password = $password;
        }
    
        /**
         * @ORM\PrePersist
         *
         * First time you save the entity
         */
        public function prePersist()
        {
            $this->password = Encrypt::cryptPassword($this->password);
        }
    }
    

    In this example, password is not encoded until you save your member object, so it can be validate by an assert !

    However Symfony2 security comes with a firewall that can encrypt the password of members and simply do the job for you:

    http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.