Hello I have one question here. Can I add and how can I add custom function to form in customType class.
My goal: I would like to be able to do something like
$myForm->myCustomFunction()
I have tried to add it directly in customType class but when I try to use it in controller like I did above I get not existing method name error. Is that possible?
Yea... as you discovered, you cannot do this. The problem is that your "type" class is kind of a "recipe". And when you call createForm(), the form component reads your recipe and creates a Form object from it. At that point, your *Type is discarded. The correct solution is to put this method somewhere else... and the right spot depends on what you want to do - common options are a private method in your controller, method in your entity or a service class. If you want a suggestion, let me know what the method does and I'll at least tell you where I would put it :).
Thanks for such quick feedback. I though that might not be the right place.
So I am trying to create convinient error wrapper. When you call $myForm->getErrors() you have to make couple of loops and a little bit of additional work to get errors. I would like to be able to call $myForm->getJsonErrors() and couple simillar methods. This would be my custom functionality shared accros all the forms.
I would like to know if I can do this "From within the form" or I have to use it as separate service and then pass data to that service? I have this second solution working but I am really keen to make first one work.
Yea, the form errors are a huge pain when you're working with an API :/. So, I feel you on that one :). But yea, the proper solution is what you have: to isolate this out to a service. But, to make it even simpler, I usually create my own base controller and add some shortcut method there, so that I can do something like $errors = $this->getJsonErrors($form). A service is really the best way to go, and together with that shortcut, it's got the "ease" of putting it on the form class (hopefully).
I believe it works the same, the only thing that changed is the location of FOSUserBundle configuration. If you installed the bundle using Symfony Flex, then you should have a fos_user.yaml file inside config/packages, just add the config logic there, and that should be it.
Hi Ryan! thanks a lot for this recipy. I need to do two form types, can i define them with fosuserbundle? I already have one, but what if i want to add another formtype?
You don't need FOSUserBundle to define your form types - you should do it in your project. Actually, we do exactly the same here - created the form type in this project, but just "based" it on the FOSUserBundle's form type and that's it. But probably it depends on what exactly form type you're talking about.
Hm, that's interesting, probably something went wrong. If we're talking about your project during development (NOT in production!), I'd recommend you to drop your DB and create it again with: bin/console doctrine:database:drop bin/console doctrine:database:create
And then, if you use migrations from scratch, run them again with: bin/console doctrine:migration:migrate
This command will create a new table in your DB named "migration_versions" (if you haven't changed its name in config) which will start tracking your migrations. Also, please make sure you don't have an entity which conflicts with this table name.
great tutorial. I have one question. I like to remove the 'username' field in the form. How can I do this? I thought just to remove getParent() function in RegistrationFormType, but that gives an error:
Cannot read index "firstName" from object of type "AppBundle\Entity\User" because it doesn't implement \ArrayAccess.
Any ideas?
Also I did your sucurity tutorial with guard. But I need a password forgotten link and a user admin area. So I thought may be FOSUserBundle would be better for that. What do you think?
Btw, in a different thread, I just replied that you might want to NOT use FOSUserBundle. Obviously, if it's working well for you, use it! But, as I said in that other comment, if it's a pain, then it may not be worth using it, just to get free "reset password" functionality.
Cheers!
Please, log in to vote for this comment
|
Share Comment
"Houston: no signs of life" Start the conversation!
17 Comments
Hello I have one question here. Can I add and how can I add custom function to form in customType class.
My goal: I would like to be able to do something like
$myForm->myCustomFunction()
I have tried to add it directly in customType class but when I try to use it in controller like I did above I get not existing method name error. Is that possible?
Regards,
Rob
Yo Robert!
Yea... as you discovered, you cannot do this. The problem is that your "type" class is kind of a "recipe". And when you call
createForm(), the form component reads your recipe and creates aFormobject from it. At that point, your *Type is discarded. The correct solution is to put this method somewhere else... and the right spot depends on what you want to do - common options are a private method in your controller, method in your entity or a service class. If you want a suggestion, let me know what the method does and I'll at least tell you where I would put it :).Cheers!
Hi Ryan,
Thanks for such quick feedback. I though that might not be the right place.
So I am trying to create convinient error wrapper. When you call $myForm->getErrors() you have to make couple of loops and a little bit of additional work to get errors. I would like to be able to call $myForm->getJsonErrors() and couple simillar methods. This would be my custom functionality shared accros all the forms.
I would like to know if I can do this "From within the form" or I have to use it as separate service and then pass data to that service? I have this second solution working but I am really keen to make first one work.
Can I or should I override main form class?
Cheers!
Yo Robert!
Yea, the form errors are a huge pain when you're working with an API :/. So, I feel you on that one :). But yea, the proper solution is what you have: to isolate this out to a service. But, to make it even simpler, I usually create my own base controller and add some shortcut method there, so that I can do something like
$errors = $this->getJsonErrors($form). A service is really the best way to go, and together with that shortcut, it's got the "ease" of putting it on the form class (hopefully).Cheers!
Hi Ryan,
Thank you for confirming that. I will definitely go with that solution. Thanks for you time and effort!
Regards,
Rob
Hi,
How to do in symfony 4.4?
Its not same structure. (services, etc)
Thank's
Hey @Medah
I believe it works the same, the only thing that changed is the location of FOSUserBundle configuration. If you installed the bundle using Symfony Flex, then you should have a
fos_user.yamlfile insideconfig/packages, just add the config logic there, and that should be it.Cheers!
Hi Ryan! thanks a lot for this recipy. I need to do two form types, can i define them with fosuserbundle? I already have one, but what if i want to add another formtype?
Hey Vadino,
You don't need FOSUserBundle to define your form types - you should do it in your project. Actually, we do exactly the same here - created the form type in this project, but just "based" it on the FOSUserBundle's form type and that's it. But probably it depends on what exactly form type you're talking about.
Cheers!
Hi Ryan, how can I add a non-mapped field to the login form of FOSUserBundle? The bundle doesn't seems to have a "LoginFormType" class?
Hey Elvism,
FOSUserBundle does not use Symfony Form for login form, see https://github.com/FriendsO... . So you can override this template to modify it.
Cheers!
When I run the doctrine:migration:migrate command it comes back with an error: 'User table or view already exists'
Hey Luke
Hm, that's interesting, probably something went wrong. If we're talking about your project during development (NOT in production!), I'd recommend you to drop your DB and create it again with:
bin/console doctrine:database:drop
bin/console doctrine:database:create
And then, if you use migrations from scratch, run them again with:
bin/console doctrine:migration:migrate
This command will create a new table in your DB named "migration_versions" (if you haven't changed its name in config) which will start tracking your migrations. Also, please make sure you don't have an entity which conflicts with this table name.
Cheers!
Hey Ryan,
great tutorial.
I have one question. I like to remove the 'username' field in the form. How can I do this?
I thought just to remove getParent() function in RegistrationFormType, but that gives an error:
Cannot read index "firstName" from object of type "AppBundle\Entity\User" because it doesn't implement \ArrayAccess.
Any ideas?
Also I did your sucurity tutorial with guard. But I need a password forgotten link and a user admin area. So I thought may be FOSUserBundle would be better for that.
What do you think?
I just saw, the next chapter is just about that.
Haha, glad you found it! Sorry to confuse you temporarily! :D
Btw, in a different thread, I just replied that you might want to NOT use FOSUserBundle. Obviously, if it's working well for you, use it! But, as I said in that other comment, if it's a pain, then it may not be worth using it, just to get free "reset password" functionality.
Cheers!
"Houston: no signs of life"
Start the conversation!