Symfony form date range plus existing date












0















I have a form which I use for creating and updating an entity. One of the attributes is a date, startDate, which input allows this year and next year. So currently, 2019 and 2020. This is my code in TestType.php:



->add('startDate', DateType::class, [
'years' => range(date('Y'), date('Y') +1),
])


This works as expected. There is one problem I face when a user wants to update an entity with a data with a year from the past, for example 2017. What I would like in that case is to change the range to that year (2017) until now + 1 year (2020). Is there any way this can be achieved in the TestType.php file?










share|improve this question























  • Do you have access to the data here that tells you you are updating existing data and what the date was in that case?

    – RiggsFolly
    Jan 3 at 11:04













  • @RiggsFolly, I can pass it from my controller.

    – Dirk J. Faber
    Jan 3 at 11:11
















0















I have a form which I use for creating and updating an entity. One of the attributes is a date, startDate, which input allows this year and next year. So currently, 2019 and 2020. This is my code in TestType.php:



->add('startDate', DateType::class, [
'years' => range(date('Y'), date('Y') +1),
])


This works as expected. There is one problem I face when a user wants to update an entity with a data with a year from the past, for example 2017. What I would like in that case is to change the range to that year (2017) until now + 1 year (2020). Is there any way this can be achieved in the TestType.php file?










share|improve this question























  • Do you have access to the data here that tells you you are updating existing data and what the date was in that case?

    – RiggsFolly
    Jan 3 at 11:04













  • @RiggsFolly, I can pass it from my controller.

    – Dirk J. Faber
    Jan 3 at 11:11














0












0








0








I have a form which I use for creating and updating an entity. One of the attributes is a date, startDate, which input allows this year and next year. So currently, 2019 and 2020. This is my code in TestType.php:



->add('startDate', DateType::class, [
'years' => range(date('Y'), date('Y') +1),
])


This works as expected. There is one problem I face when a user wants to update an entity with a data with a year from the past, for example 2017. What I would like in that case is to change the range to that year (2017) until now + 1 year (2020). Is there any way this can be achieved in the TestType.php file?










share|improve this question














I have a form which I use for creating and updating an entity. One of the attributes is a date, startDate, which input allows this year and next year. So currently, 2019 and 2020. This is my code in TestType.php:



->add('startDate', DateType::class, [
'years' => range(date('Y'), date('Y') +1),
])


This works as expected. There is one problem I face when a user wants to update an entity with a data with a year from the past, for example 2017. What I would like in that case is to change the range to that year (2017) until now + 1 year (2020). Is there any way this can be achieved in the TestType.php file?







php symfony






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 10:54









Dirk J. FaberDirk J. Faber

1,3561317




1,3561317













  • Do you have access to the data here that tells you you are updating existing data and what the date was in that case?

    – RiggsFolly
    Jan 3 at 11:04













  • @RiggsFolly, I can pass it from my controller.

    – Dirk J. Faber
    Jan 3 at 11:11



















  • Do you have access to the data here that tells you you are updating existing data and what the date was in that case?

    – RiggsFolly
    Jan 3 at 11:04













  • @RiggsFolly, I can pass it from my controller.

    – Dirk J. Faber
    Jan 3 at 11:11

















Do you have access to the data here that tells you you are updating existing data and what the date was in that case?

– RiggsFolly
Jan 3 at 11:04







Do you have access to the data here that tells you you are updating existing data and what the date was in that case?

– RiggsFolly
Jan 3 at 11:04















@RiggsFolly, I can pass it from my controller.

– Dirk J. Faber
Jan 3 at 11:11





@RiggsFolly, I can pass it from my controller.

– Dirk J. Faber
Jan 3 at 11:11












1 Answer
1






active

oldest

votes


















2














You can set the start year on pre set data event like:



$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->add('startDate', DateType::class, [
'years' => range(
$event->getData()->getStartDate() ?
$event->getData()->getStartDate()->format('Y') :
date('Y'),
date('Y')+1
),
]);
});


Symfony's form events documentation






share|improve this answer


























  • Seems to work well. The only issue I faced was the getYear() method was not recognised. I changed this by simply formatting the date, replacing getYear() with format('Y').

    – Dirk J. Faber
    Jan 3 at 11:25











  • Answer updated. Don't know why I wrote getYear... Dont forget to mark your question as "Answered" ;)

    – G1.3
    Jan 3 at 11:28











  • of course ;) Thank you for this solution.

    – Dirk J. Faber
    Jan 3 at 11:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020901%2fsymfony-form-date-range-plus-existing-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can set the start year on pre set data event like:



$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->add('startDate', DateType::class, [
'years' => range(
$event->getData()->getStartDate() ?
$event->getData()->getStartDate()->format('Y') :
date('Y'),
date('Y')+1
),
]);
});


Symfony's form events documentation






share|improve this answer


























  • Seems to work well. The only issue I faced was the getYear() method was not recognised. I changed this by simply formatting the date, replacing getYear() with format('Y').

    – Dirk J. Faber
    Jan 3 at 11:25











  • Answer updated. Don't know why I wrote getYear... Dont forget to mark your question as "Answered" ;)

    – G1.3
    Jan 3 at 11:28











  • of course ;) Thank you for this solution.

    – Dirk J. Faber
    Jan 3 at 11:30
















2














You can set the start year on pre set data event like:



$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->add('startDate', DateType::class, [
'years' => range(
$event->getData()->getStartDate() ?
$event->getData()->getStartDate()->format('Y') :
date('Y'),
date('Y')+1
),
]);
});


Symfony's form events documentation






share|improve this answer


























  • Seems to work well. The only issue I faced was the getYear() method was not recognised. I changed this by simply formatting the date, replacing getYear() with format('Y').

    – Dirk J. Faber
    Jan 3 at 11:25











  • Answer updated. Don't know why I wrote getYear... Dont forget to mark your question as "Answered" ;)

    – G1.3
    Jan 3 at 11:28











  • of course ;) Thank you for this solution.

    – Dirk J. Faber
    Jan 3 at 11:30














2












2








2







You can set the start year on pre set data event like:



$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->add('startDate', DateType::class, [
'years' => range(
$event->getData()->getStartDate() ?
$event->getData()->getStartDate()->format('Y') :
date('Y'),
date('Y')+1
),
]);
});


Symfony's form events documentation






share|improve this answer















You can set the start year on pre set data event like:



$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->add('startDate', DateType::class, [
'years' => range(
$event->getData()->getStartDate() ?
$event->getData()->getStartDate()->format('Y') :
date('Y'),
date('Y')+1
),
]);
});


Symfony's form events documentation







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 11:27

























answered Jan 3 at 11:12









G1.3G1.3

993213




993213













  • Seems to work well. The only issue I faced was the getYear() method was not recognised. I changed this by simply formatting the date, replacing getYear() with format('Y').

    – Dirk J. Faber
    Jan 3 at 11:25











  • Answer updated. Don't know why I wrote getYear... Dont forget to mark your question as "Answered" ;)

    – G1.3
    Jan 3 at 11:28











  • of course ;) Thank you for this solution.

    – Dirk J. Faber
    Jan 3 at 11:30



















  • Seems to work well. The only issue I faced was the getYear() method was not recognised. I changed this by simply formatting the date, replacing getYear() with format('Y').

    – Dirk J. Faber
    Jan 3 at 11:25











  • Answer updated. Don't know why I wrote getYear... Dont forget to mark your question as "Answered" ;)

    – G1.3
    Jan 3 at 11:28











  • of course ;) Thank you for this solution.

    – Dirk J. Faber
    Jan 3 at 11:30

















Seems to work well. The only issue I faced was the getYear() method was not recognised. I changed this by simply formatting the date, replacing getYear() with format('Y').

– Dirk J. Faber
Jan 3 at 11:25





Seems to work well. The only issue I faced was the getYear() method was not recognised. I changed this by simply formatting the date, replacing getYear() with format('Y').

– Dirk J. Faber
Jan 3 at 11:25













Answer updated. Don't know why I wrote getYear... Dont forget to mark your question as "Answered" ;)

– G1.3
Jan 3 at 11:28





Answer updated. Don't know why I wrote getYear... Dont forget to mark your question as "Answered" ;)

– G1.3
Jan 3 at 11:28













of course ;) Thank you for this solution.

– Dirk J. Faber
Jan 3 at 11:30





of course ;) Thank you for this solution.

– Dirk J. Faber
Jan 3 at 11:30




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020901%2fsymfony-form-date-range-plus-existing-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'