0

i have a question regarding symfony 2 validation via an Entity and a validation configuration file. Here is the explanation of the problem.

I have an entity for the users account settings which has 5 properties - subdomain, email, oldPassword, password and new_password_confirmed.

My configuration file is as follows:

Backend\Builders\PageBundle\Entity\AccountSettings:
properties:
    email:
        - Email: ~
    oldPassword:
        - Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
            message: "Wrong value for your current password"
constraints:
        - Expression:
            expression: "this.passwordMatch()"
            message: "Passwords don`t match"  

My question is - How can i use this entity for validation and choose to perfom the validation only on a certain item, for example the email . Here is my code in the moment for this validation, but it requires all the validation rules to be met in order the validation to be successfull:

    $accountSettings = new AccountSettings();
    $accountSettings->email = $_POST['email'];
    $validator = $this->get('validator');
    $errors = $validator->validate($accountSettings);

1 Answer 1

2

You should then use validation groups. It allows you to do,

Backend\Builders\PageBundle\Entity\AccountSettings:
properties:
    email:
        - Email: { groups: [xxx_group] }
    oldPassword:
       # ...

$errors = $validator->validate($accountSettings, array('xxx_group'));

Check the example provided in the documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.