php - Validation based on another field -


I am new to this Symfony framework and a dead ended during implementation. I'll have to validate the new password and password confirmation field only if the user's current password is entered.

  • / Ul>

    but turns either the sections used are removed or any unit is required.

    The implementation of the two fields is as follows,

      // If this field is loaded $ builder-gt; ('Current password', 'password', array ('label' = & gt; 'current password', 'expected' = & gt; false, 'attr' = & gt; array ('class' => Form-control "error," '=' '))); // This must be filled in fields or set as the required $ builder- & gt; Add ('password', 'duplicated', array ('type' = & gt; 'password', 'required' = & gt; wrong, invalid_message = '> error message: PASSWORDS_DO_NOT_MATCH,' options' => ; Array ('atri' => array ('class' = & gt;' password-field form-control ')),' first_ option '=> array (' label '= & gt; wrong;' Error_bubbling '= & gt; true,' label_attr '= & gt; array (' class' = & gt; 'col-sm-4-control-label')), 'seconds_points' = & gt; array (' label ' = & Gt; wrong, 'label_attr' = & gt; array ('square' = & gt; 'col-sm-4-control-label')));   

    I have applied a verification using a set of conditions within the controller if but it would be best to learn the correct way to run assumptions for the scenario such as this .

    Edit

    User entity

      

    & Lt ;? Php namespace prose bundle \ AccountsBundle \ Entity; As the Symfony \ Component \ Validator \ Constraints use; Use Symfony \ Component \ Security \ Core \ User \ UserInterface; Use the bundle \ accountsBundle \ Custom \ ErrorMessages; Class user user interface, \ serializable {/ ** * @Assert \ email (message = error message: EMAIL_ADDRESS_INVALID) applies * @Assert \ NotBlank (message = error message: EMAIL_ADDRESS_EMPTY) * / private $ email; / ** * @Assert \ NotBlank (Message = Error Message: PASSWORD_EMPTY, Group = {"Full"}) * / Private $ Password; Private $ old password; Private $ ID; Private $ userId; Private $ name; Private $ username; Public function __ composition () {} function set email ($ email) {$ this- & gt; Email = $ email; $ This- & gt; User name = $ email; } GetEmail () function {return $ this-> E-mail; } Function Set Password ($ password) {$ this- & gt; Password = $ password; } GetPassword function () {return $ this-> password; } Function Set Old Password ($ old password) {$ this- & gt; Old password = $ old password; } GetOldPassword function () {return $ this- & gt; Old Password; } Function setId ($ id) {$ this- & gt; Id = $ id; } Function getId () {return $ this- & gt; Id; } Function Set User ID ($ user-) {$ this- & gt; UserId = $ userId; } GetUserId () function {return $ this- & gt; UserId; } Function Set Name (name name $ name) {$ this- & gt; Name = $ name; } GetName function () {return $ this-> Name; } Delete public functionsCredential () {} public function getRoles () {return array ('ROLE_USER'); } Public function getSalt () {} Public Function getUsername () {Return $ this- & gt; User name; }}

    Modify your class as follows

      Use Symfony \ Component \ Validator \ Constraints as; Use Symfony \ Component \ Validator \ ExecutionContext; / ** * @@Asset callback (methods = {"verify password"}) / Class user user interface, \ serializable {// all your old code here Public Verify Password (Execution Contex $ $ Reference) {/ In case of your control failure about / password, you can add that snippet of field // $ code-> addViolationAtPath ($ propertyPath, 'your message here', array (), null); }}   

    Of course you have to be able to enter all the notifications in the passwordVerify function and the fastest way to do this is to create the field verifyPassword in your unit, so when you form the form with the unit, all the data will be there.

    When you use isValid () , this snippet of code will automatically call the callback method

  • Comments

    Popular posts from this blog

    java - ImportError: No module named py4j.java_gateway -

    python - Receiving "KeyError" after decoding json result from url -

    .net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -