简体   繁体   中英

How to make a field that is an entity as hidden in the form with SonataAdmin?

I need to add entity field as a hidden in my form In the admin form with sonata I have:

protected function configureFormFields(FormMapper $formMapper)
    {    
        if ($this->getRoot()->getSubject()->getId()) {
            $formMapper
                ->add('driverNight', 'hidden', array(), array('admin_code' => 'cab.admin.driver'))
            ->add('monday', 'checkbox', array('required' => false, "attr" => array('class' => 'checkbox-day'), 'label' => 'Monday', 'value' => '0'))
            ->add('tuesday', 'checkbox', array('required' => false, "attr" => array('class' => 'checkbox-day'), 'label' => 'Tuesday', 'value' => '0'))
            ->add('wednesday', 'checkbox', array('required' => false, "attr" => array('class' => 'checkbox-day'), 'label' => 'Wednesday', 'value' => '0'))
            ->add('thursday', 'checkbox', array('required' => false, "attr" => array('class' => 'checkbox-day'), 'label' => 'Thursday', 'value' => '0'))
            ->add('friday', 'checkbox', array('required' => false, "attr" => array('class' => 'checkbox-day'), 'label' => 'Friday', 'value' => '0'))
            ->add('saturday', 'checkbox', array('required' => false, "attr" => array('class' => 'checkbox-day'), 'label' => 'Saturday', 'value' => '0'))
            ->add('sunday', 'checkbox', array('required' => false, "attr" => array('class' => 'checkbox-day'), 'label' => 'Sunday', 'value' => '0'))

}

Actually, no value in the input (see the inspect image)

HTML中的检查字段

How to make the driverNight field as hidden knowing that it is of type entity?

You could use a data transformer as detailed here: data transformer

I suggest that you create a transformer class which transforms the entity to an int in the view layer and reverses from int to entity in the model layer and the use the addModelTransformer method in your config form.

  1. Create the model Transformer class which implements Symfony\\Component\\Form\\DataTransformerInterface and contains 2 methods transform and reverseTransform . This class could be constructed by passing 2 arguments: Entity manager and the user entity object that will be transformed to int.

.

class DriverToIntTransformer implements DataTransformerInterface {
private $manager;
private $driver;

public function __construct(ObjectManager $manager, $oUser)
{
    $this->manager = $manager;
    $this->driver = $oUser;
}
public function transform($user)
{
    if (null === $user) {
        return $this->driver->getId();
    }
    return $user->getId();
}
public function reverseTransform($driverNumber)
{
    if (!$driverNumber) {
        return;
    }
    $oDriver = $this->manager
        ->getRepository('YourUserBundle:User')
        // query for the driver with this id
        ->find($driverNumber);
    if (null === $oDriver) {
        throw new TransformationFailedException(sprintf(
            'An user with number "%s" does not exist!',
            $driverNumber
        ));
    }
    return $oDriver;
}

2 call the transform method:

if ($this->getRoot()->getSubject()->getId()) {
        $driver = $this->getRoot()->getSubject()->getId(); 
$em = $this->container->get('doctrine.orm.entity_manager');
        $oDriver = $em->getRepository('YourUserBundle:User')->find($driver);
        $formMapper
            ->add('driverNight', 'hidden', array(), array('admin_code' => 'cab.admin.driver'));
        $formBuilder = $formMapper->getFormBuilder();
        $formBuilder->get('driverNight')->addModelTransformer(new DriverToIntTransformer($em, $oDriver));



    $formMapper
        ->add('monday', 'checkbox', array('required' => false, "attr" => array('class' => 'checkbox-day'), 'label' => 'Monday', 'value' => '0'));//.....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM