olga
olga

Reputation: 969

Symfony 2.7. How to get/set value of the Form field after Form submission?

I want to set the value (unix timestamp) for the form hidden datetime element after the form submission, Symfony 2.7.

$starttimeStr=$form->get('meetingbundle_event[starttimeStr]');// here i intend to get user input for the date in string format
$dateObj = new \Datetime($starttimeStr); 
$starttimeInt=$dateObj->getTimestamp();
$form->get('meetingbundle_event[starttimeInt]')->setData($starttimeInt); //here i want to set  the datetime in decimal format

But this does not work, because nor id meetingbundle_event_starttime, nor name meetingbundle_event[starttimeStr] are not accepted as valid arguments for get(string $name) of FormInterface: http://api.symfony.com/3.0/Symfony/Component/Form/FormInterface.html I know name and id by using Tools-> WebDeveloperExtension->Forms->DisplayFormDetails in Firefox.

Thus i tried $elem=$form->all(); to see what are the names of my form elements, but i can not see the result:

print_r($elem); // crashes web-browser
$fs->dumpFile('C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj2_27\form.txt', $elem ); */ Complains that there is no memory

$elemser=  serialize($elem); //gives error that  php can not serialize Closure

$elemjson = json_encode($elem);
$fs->dumpFile('C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj2_27\form.txt', $elemjson ); // outputs empty strings

What is the default naming rules for Form fields in order i could retrieve them using $form->get('field_name')?

Upvotes: 0

Views: 2062

Answers (3)

Max P.
Max P.

Reputation: 5679

To get element value use $form->getData()['%element_name%']
As I know it is impossible to change submitted form element value explicit by calling $form->setData(). So if you want to change submitted form element value inside form use FormBuilder method addViewTransformer http://symfony.com/doc/current/cookbook/form/data_transformers.html or do it in form event listener http://symfony.com/doc/current/components/form/form_events.html.

Upvotes: 1

olga
olga

Reputation: 969

Finally! One has to get field, than to get Data. It is not described in Official documentation.

$starttimeStr=$form->get('starttimeStr')->getData();

print_r($starttimeStr); // gives user input "21-12-2015 04:33 Europe/London".

Upvotes: 0

olga
olga

Reputation: 969

Found haw to get the child of form - use name from FormType class (not field id nor name in the form):

$starttimeStr=$form->get('starttimeStr'); //where starttimeStr is the corresponding name of the field in Form class "\src\Bundle\Form\EventType.php".

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', 'text', array(  'attr' => array( 'style' => 'width: 500px',), ) )
        ->add('keywords', 'text', array(  'attr' => array('style' => 'width: 500px',) ,) )
        ->add('starttime', 'hidden', array('data' => '0',) )
        ->add('endtime', 'hidden', array('data' => '0',) ) 
        ->add('starttimeStr') ...

But still i am not able to get the value. $starttimeStr appears to be not the simple input date string (21-12-2015), but large array which crashes Firefox.

print_r($starttimeStr);// crashes Firefox.

$elemser=serialize('starttimeStr');
$fs->dumpFile('C:\Bitnami\wampstack-5.5.30-0\sym_prog\proj2_27\form.txt', $elemser ); // outputs empty string "s:12:"starttimeStr";"

Upvotes: 0

Related Questions