简体   繁体   中英

sonata admin - get post data

I'm learning symfony2 and sonata admin and came across few problems and this is one of them.
I've created an admin class which extends sonata admin and the below wouldn't work for me:

$this->getForm()->get('page')

or

$this->getRequest()->request->get('page')

I'm trying to pass some hidden fields in the configureFormFields but I can't access them using the above after the form has been submitted. I can see the request array but get('page') returns null. Also, the request array is multidimensional.

Any advice appreciated.

Simple example of what I'm trying to do is below:

protected function configureFormFields(FormMapper $formMapper)  
{  
    $formMapper  
        ->add('title')  
        ->add(  
            'subobject',  
            'hidden',  
            array(  
                'mapped' => false,  
                'data' => 'sub'  
            )  
        )  
    ;  
}  
public function prePersist($object)  
{  
    $subobject_request = $this->getRequest()->request->get('subobject');  
    print_r($subobject_request); //is null  
    die();  
}  

也许有点晚了,但我希望它对某人有所帮助:

$this->getForm()->get('subobject')->getData()

Your attempt looks good. Looking at this post: https://groups.google.com/forum/#!topic/sonata-users/NS0mTAAHt7o

I could successfuly use:

public function preUpdate($object)
{
    $uniqid = $this->getRequest()->query->get('uniqid');
    $formData = $this->getRequest()->request->get($uniqid);
    var_dump($formData);exit;
}

to retrieve all submitted elements.

i'm not sure , but for mapped field you get the values directy in the parameter of your prepersist .

did you try to access your 'sub' values directly from $object ? like

$object->sub;

The $this->getRequest() works for symfony but i think it's a little bit different in sonata admin bundle ..

Just an example for relation Office > Plan and looking for prop Plan.nameEn .
In OfficeAdmin.php :

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper->add('plan', 'sonata_type_admin')
}

public function preUpdate($marina) {
    if ($planName = $this->getForm()->get('plan')->get('name_en')->getData()) {}
}

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