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 8869061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:23:42+00:00 2026-06-14T17:23:42+00:00

I’m a Symfony noob trying unsuccessfully to visualize how best to validate a form

  • 0

I’m a Symfony noob trying unsuccessfully to visualize how best to validate a form field based on either it or a different field. The case: a form will solicit either a date of birth or an age. If the dob is entered, age is ignored. If age is entered and dob is empty, the dob is said to be today’s date less age in years. If neither is entered a validation error is thrown. I’ve accomplished this with Smarty validation; as a learning exercise I’m trying to reproduce the application in Symfony.

I’ve looked at this solution where both fields are properties of an entity. In my case age is not, only dob. So it’s not clear to me how to apply that solution. I’d greatly appreciate pointers.

Thanks.

George
PS: (edited: dreck removed)
PPS: (edited: removed to make room for nearly working version)
Form:

// src\Mana\AdminBundle\Resources\views\Form\Type\NewClientType.php
namespace Mana\AdminBundle\Form\Type;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Mana\AdminBundle\Validator\Constraints;

class NewClientType extends AbstractType {

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array('validation_groups' => 'client_new', 
            'validation_constraint' => new DOBorAge(),
            ));
     }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('fname', null, array('required' => false));
        $builder->add('sname', null, array('required' => false));
        $builder->add('dob', 'birthday', array('widget' => 'single_text', 'required' => false));
        $builder->add('age', null, array('mapped' => false, 'required' => false));
    }

    public function getName() {
        return 'client_new';
    }
}

services:

services:
  client_new:
    class: Mana\AdminBundle\Validator\Constraints\DOBorAgeValidator
    scope: request
    tags:
      - { name: validator.constraint_validator, alias: dobage_validator}

Validators:

// src\Mana\AdminBundle\Form\Type\DOBorAge.php
namespace Mana\AdminBundle\Form\Type;
use Mana\AdminBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

class DOBorAge extends Constraint {

    public $message = 'Either a date of birth or age must be present';

    public function validatedBy() {
        return 'dobage_validator';
    }

    public function getTargets() {
        return Constraint::CLASS_CONSTRAINT;
    }

}

and

// src\Mana\AdminBundle\Validator\Constraints\DOBorAgeValidator.php
namespace Mana\AdminBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class DOBorAgeValidator extends ConstraintValidator {

    protected $request;

    public function __construct(Request $request) {
        $this->request = $request;
    }

    public function validate($value, Constraint $constraint) {
        var_dump($this->request->request->get('client'));
        die();
        return true;
    }
}
  • 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-14T17:23:43+00:00Added an answer on June 14, 2026 at 5:23 pm

    A Symfony-esque solution: a single form field that takes either a date or an age, then transform the entry into a date. (Now, if I could only turn this into a custom field…)

    namespace Mana\AdminBundle\Form\Type;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Mana\AdminBundle\Form\DataTransformer\AgeToDOB;
    
    class NewClientType extends AbstractType {
    
        public function buildForm(FormBuilderInterface $builder, array $options) {
    
            $transformer = new AgeToDOB();
    
            $builder->add('fname', null, array('required' => false, 
                'invalid_message' => 'First name not be blank',));
            $builder->add('sname', null, array('required' => false,
                'invalid_message' => 'Last name not be blank',));
            $builder->add(
                    $builder->create('dob', 'text', array('required' => false,
                ))->addModelTransformer($transformer));
        }
    
        public function getName() {
            return 'client_new';
        }
    }
    

    and the transformer:

    namespace Mana\AdminBundle\Form\DataTransformer;
    
    use Symfony\Component\Form\DataTransformerInterface;
    use Symfony\Component\Form\Exception\TransformationFailedException;
    use Mana\AdminBundle\Entity\Client;
    
    class AgeToDOB implements DataTransformerInterface {
    
        public function reverseTransform($dob) {
    
            if (null == $dob) {
                return '';
            }
            if ((substr_count($dob, '/') == 2 && strtotime($dob))) {
                $date = new \DateTime($dob);
                return date_format($date, 'Y-m-d');
            }
            if (is_numeric($dob)) {
                $date = new \DateTime();
                $interval = 'P' . $dob . 'Y';
                $date->sub(new \DateInterval($interval));
                return date_format($date, 'Y-m-d');
            }
        }
    
        public function transform($client) {
            if (null == $client) {
                return '';
            }
            if (is_object($client)) {
                $dob = $client->getDob();
                // date: test for two / characters
                if ((substr_count($dob, '/') == 2 && strtotime($dob))) {
                    $date = new \DateTime($dob);
                    return date_format($date, 'm/d/Y');
                }
                if (is_numeric($dob)) {
                    $date = new \DateTime();
                    $interval = 'P' . $dob . 'Y';
                    $date->sub(new \DateInterval($interval));
                    return date_format($date, 'm/d/Y');
                }
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I have a text area in my form which accepts all possible characters from

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.