Symfony form date range plus existing date
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
add a comment |
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
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
add a comment |
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
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
php symfony
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
Seems to work well. The only issue I faced was thegetYear()method was not recognised. I changed this by simply formatting the date, replacinggetYear()withformat('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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Seems to work well. The only issue I faced was thegetYear()method was not recognised. I changed this by simply formatting the date, replacinggetYear()withformat('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
add a comment |
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
Seems to work well. The only issue I faced was thegetYear()method was not recognised. I changed this by simply formatting the date, replacinggetYear()withformat('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
add a comment |
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
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
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 thegetYear()method was not recognised. I changed this by simply formatting the date, replacinggetYear()withformat('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
add a comment |
Seems to work well. The only issue I faced was thegetYear()method was not recognised. I changed this by simply formatting the date, replacinggetYear()withformat('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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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