I have a form that contains two fields to upload files. I need to execute the following points:
- validate the form;
- move files to another directory;
- handle the moved files;
- return the result of handling.
Also I must return error messages if one of above points was failed. I am confused how to do that more flexible. I don’t want to do all in controller.
I created form handler, form model, form type as in FOSUserBundle and added validation rules to form model in validation.yml. Form validation works fine.
I have an idea to use event dispatcher for this issue. After form validation I can notify event listeners.
What do you think?
It depends.
If you need to subscribe to the event because you need to perfom different actions (e.g. email the support, clean up trash, write down a failure log in the database), use event dispatcher. But if it’s a simple thing like just display an error, it makes sense to use exceptions instead:
Another option depends on what ‘handle the moved files’ is. If it’s a part of validation (handle them to find that they are valid ssh keys, for example) and not business logic, then you can create a new validator and it’ll become a part of form validation.