How to Fix "Attempted to call an undefined method named "createNamedBuilder..."? - symfony-forms

I'm trying to create a named form (builder) within my controller like
...
$form = $this->createNamedBuilder('form1', $data)
->add(...)
->getForm();
But i get the title-mentioned error.
When i check the abstract controller trait class there are no createNamed() or createNamedBuilder() functions in there.
How do i create a named form with a form builder?
Kind Regards

According to This you need to acquire the FormFactory(Interface via Dependency Injection).
Adding FormFactoryInterface $formFactory to my controller method's parameters and using it like
$formFactory->createNamedBuilder('name', FormType::class, $data)...
did the trick.

Related

What calls Controller class's methods in MVC

A general question in MVC. We know, in order to call a method, an object need to be created for the class containing the method and call then call it with that object.
My question is, in MVC we only define the default controller name and 'ActionResult' in route config.
Now what is creating object for that controller class and calling method(ActionResult) with that object?
It's the MVC framework itself. Based on the route, method etc it's supposed to handle, it will instantiate a particular controller class and invoke the specified method/s for it to handle that route.

Grails Custom Scaffolding get access to controller name

I am trying to write a custom src/templates/scaffolding/Controller.groovy and was wondering if there was any way to get access to the controller name? Right now it seems like you can only get the "model" class. The reason I need it is I am customizing the render to prefix the templates directory based on the controller.
For instance I have a controller named AuthorAdminController and I need to customize the list to use the /admin/user/** directory.
Let me know if you have any questions. I am getting ready to look into how to customize DefaultGrailsTemplateGenerator but I am not sure if that is the correct route to go.
Example:
class UserAdminController {
static scaffold = User
}
Currently in my Controller.groovy I get className='user' so I have no access to the controller.
I don't think you can, as the way scaffolding works your template will always be generating a class named DomainClassNameController (i.e. UserController in your example), which gets loaded into a new classloader and then the metaclass of the real controller (UserAdminController) gets new actions added to it which delegate to an instance of the generated UserController.
Now every controller has access to the controllerName property during execution of actions, so this may provide you with a workaround. I haven't tried it, but you could try putting a log.info("controller: \${controllerName}") into the template and see which name it gives you (the backslash to make it resolve at runtime rather than generation time).

Get route name from inside view?

Using Attribute Routing you can define a route name. Is there a way I can get the name of the used route from inside my view?
According to this answer, you can use:
#{ var x = ViewContext.RouteData.Values["Action"]; }
to get route data. Then according to the right answer of this post: "How to get a custom attribute from object instance in C#", you can pull out the attributes.
This doesn't sound like a real good idea. You should probably add the route name to view data or the view model instead.
MapMvcAttributeRoutes has multiple overloads which expects a parameter for IDirectRouteProvider implementation. This interface is responsible for generating routes. The default implementation for this interface is DefaultDirectRouteProvider. Create a custom class that inherits from DefaultDirectRouteProvider and override required methods like GetActionDirectRoutes or GetControllerDirectRoutes. In this method you will get RouteEntry which contains the route name. You can add this name to datatokens.

Why does Grails' scaffolded create action use params?

I created a static scaffolding for my domain class and got a controller for that domain class. The create action of the controller looks like:
def create() {
[userInstance: new User(params)]
}
I wonder why the line:
[userInstance: new User(params)]
has been added. Obviously when the create action is invoked, there wont be any params - so why would this line have been added?
Obviously when the create action is invoked, there wont be any params
Not necessarily - imagine a situation where you want to pre-populate a couple fields in the form of the create view. You could use:
/app/user/create?username=myusername
Which would result in the view's userInstance having a populated username field for display in the form.
This is fundamentally by Spring, the action is called 'binding' and is the action of tie form elements from one jsp(gsp in this case) to the properties of an object and viceversa.
To tie an object to a form, well, you should create it first, how can ypu tie a null object? it's not possible, that is why the new ClassObject(...)
After that in Groovy we have POGO's, and one feature of POGO's is the ability of initialize them with a map, for example:
new User(name:'John',lastname:'Zuñiga')
But in this case there's a lil' of groovy magic with that 'params' object. That comes from Groovy Servlets or Groovlets. How can you obtain a param with Java incoming from a form? Well, with request.getParam("param_name"), but in this case with Groovy you receive a params object, this params object is a map, a Groovy map...Uhm, one second...
If POGO's in Groovy is able to receive a Map as constructor, and the params object is a Map...maybe...oh coolness I can put that map in the constructor of my object, and after Spring do the binding to the form with this new Object, so this object is travelling in actions from this controller so it comes with the properties populated.
I hope this explanation be clear, if you have questions, I'm here...
Regards
There could be params, although in general there wouldn't be.
It allows pre-loading of values, which may be helpful sometimes, including re-displaying the create form.

ruby on rails Controller class instantiate a ruby class?

I am new to RoR .I want my controller to instatiate an existing class from lib .
Collect data in the form of a string and throw the result on the view.erb.Can I do that.
Do i have to create a new model object and that should that model object inturn call the lib class.
Not really sure what you want to do.
If you used a library class - a module for example - its automatically instantiated, when you use 'include'
If you just have a generic class, and you included it somewhere, then you already have the class object loaded and can call methods on it. Or you just create an instance manually with 'object = new MyClass'.
And then call whatever you like on 'object'.
Whatever Information you collect inside the controller method, you can access in the view, when you place an '#'-Symbol before your variable.
So if you want your show.html.erb look like this:
<h1>My String:</h1>
<%= #mystring %>
then you have to do something like this in your controller:
def show
...
#mystring = MyClass.get_my_cool_string
...
end
Hope that helps...

Resources