0

I have a Roster entity in my Symfony project which contains two datetime fields.

Input field on FormType uses two single_text boxes, one for date one for time.

 $builder->add('serviceUserId', EntityType::class, array('class' => 'AppBundle:ServiceUser',
            'data' => $options['serviceUser']))
            ->add('rosterStartTime', DateTimeType::class,
                array('date_widget' => "single_text",
                    'time_widget' => "single_text",
                ))
            ->add('rosterEndTime', DateTimeType::class, array('date_widget' => "single_text",
                'time_widget' => "single_text",
            )

Can anyone tell me how to format the date I'm passing to this field in my Unit Test.

The nearest I've gotten is

      $client = static::createClient(array(), array(
        'PHP_AUTH_USER' => 'username',
        'PHP_AUTH_PW' => 'password',
    ));

    $date = new \DateTime('2015-04-01 08:00');

    //browse to an exsting service user and add a roster to that record
    $crawler = $client->request('GET', '/serviceuser/2');
    $crawler = $client->click($crawler->selectLink('Add New Roster')->link());

    // Fill in the form and submit it
    $form = $crawler->selectButton('Create')->form(array(
        'appbundle_roster[serviceUserId]' => 2,
        'appbundle_roster[rosterStartTime]'  => $date->format('yyyy-MM-dd'),

        'appbundle_roster[rosterStatus ]' => 1,
        'appbundle_roster[numberResourcesNeeded]' => 2,
        'appbundle_roster[customerId]' => 2,
    ));

It's failing with this error

     AppBundle\Tests\Controller\RosterControllerTest::testCompleteScenario
     InvalidArgumentException: Cannot set value on a compound field "appbundle_roster[rosterStartTime]".

1 Answer 1

1

Can you try this instead:

'appbundle_roster[rosterStartTime][year]' => 2015,
'appbundle_roster[rosterStartTime][month]' => 4,
'appbundle_roster[rosterStartTime][day]' => 1,

It sounds (from error message) like it's rendering as selectable drop-down lists instead of a input text field.

Sign up to request clarification or add additional context in comments.

6 Comments

Many thanks- unfortunately that returns Unreachable field [year]
So I'm really trying to help, but what is actually rendered? Can you check in a browser and then right click on the rosterStartTime field and see what the "name" is shown as? I'm presuming name is for example appbundle_roster[rosterStartTime][year]. That might help you figure out what to use.
Seems to be a date and time in two separate parts <td><div id="appbundle_roster_rosterStartTime"><input type="date" id="appbundle_roster_rosterStartTime_date" name="appbundle_roster[rosterStartTime][date]" required="required" value="2017-03-04" /><input type="time" id="appbundle_roster_rosterStartTime_time" name="appbundle_roster[rosterStartTime][time]" required="required" value="08:50" /></div></td>
thanks @Alvin this worked 'appbundle_roster[rosterStartTime][date]'=>'2017-04-01', 'appbundle_roster[rosterStartTime][time]'=>'08:00', 'appbundle_roster[rosterEndTime][date]'=>'2017-04-01', 'appbundle_roster[rosterEndTime][time]'=>'09:00'));
I was just going to suggest something like that! Glad you got it working!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.