Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Symfony form Constraints CallBack 2 parameters

I am trying to add a constraint with 2 parameters in my Symfony but i don't succeed. My code :

The form field I am trying to check if definied like that :

  ->add(
                self::CUSTOMER_FAMILY_CUSTOMFIELD_FIELD_NAME,
                'text',
                array(
                  'constraints' => array(
                      new Constraints\Callback(
                          array(
                              'methods' => array(
                                  array(
                                      $this, 'checkField'
                                  )
                              )
                          )
                      )
                  ),
                    'required' => true,
                    'empty_data' => false,
                    'label' => self::trans('Some number'),
                    'label_attr' => array(
                        'for' => 'customfield'
                    ),
                    'mapped' => false,
                )
            )

Don't care about variable in capital letter it's Thelia CMS variable which are well defined.

Then my function checkField if defined like that :

public function checkField($field, ExecutionContextInterface $context)
    {
  if(strlen($field)!=0)$context->addViolation(self::trans('FIELD ERROR')}

This is working fine. Then I would like to add a second parameters in my constraint which is a Thelia CMS object ($event). But I don't find how to do that with this code structure.

Any help is welcome.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As I see your issue, I can see these possible cases:

  1. Your $event object does not depend on the CUSTOMER_FAMILY_CUSTOMFIELD_FIELD_NAME field value:

In this case, you can just set your $event object as an attribute of the class containing your checkField function, and use its value in the function.

  1. The $event object depends on the value of the field:

In this case, you can just do the same as previous, and then, set you $event attribute in a form event callback, where you'll also redefine the field so that it's updated at the desired form event.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!