Advanced configuration of CRUD forms and controllers in Play

What I'm looking for is the ability to quickly (DRY!) Generate forms for these models, but in a less controlled way than using CRUD forms / models; for example, being able to use crud tags without complete crud controllers / routes or by strongly configuring them.

Let me explain an example.

I have model A than links (ManyToOne) for two models, B and C

class public A extends Model { public String name; @ManyToOne public A a; @ManyToOne public B b; } 

I would like to write the following routes:

 /A/{id}/B/ somecontroller /A/{id}/C/ some(other?)controller 

or even better:

 /A/{id}/{submodel}/ somecontroller 

And in the corresponding html view you can do something like:

 <div>object.name</div> #{form action:@save(object.b._key()), enctype:'multipart/form-data'} #{crud.form object.b /} <p class="crudButtons"> <input type="submit" name="_save" value="&{'crud.save', type.modelName}" /> <input type="submit" name="_saveAndContinue" value="&{'crud.saveAndContinue', type.modelName}" /> </p> #{/form} 

Where the "object" is not an instance of "b" or "c", but "a", and I can tell #{crud.form /} which model it should display (in this case, "b")

Is there a way to achieve something like this?

The issue can be resolved either:

  • is there a simple tag #{form MODEL} #{/form} ?!

or, being able to somehow customize more CRUD, for example.

  • Is there a way to change the main crud module and override only the methods you need (without copying all this!)?

I am afraid that I will not be able to achieve this goal by simply redefining the CRUD controller model, maybe I'm wrong, but besides reading the CRUD code (which I do) the official document is a bit limited by what methods can be redefined and how ...

Related: how to create an html form for a model in playframework

I also found this google mail flow which seems to go into the-crud direction setting. I was hoping for more turnkey solutions for such a typical need ...

+6
source share
1 answer

I found a game in this ! google group thread the answer I was looking for .. everything is already there, although not documented!

It is as simple as using:

to display the form for creating the Model class.

 #{crud.form class:'models.ModelName' /} 

to display the edition form of any existing copy

 #{curd.form object:anyInstance /} 

Then you can go as you want, but this is my template for editing existing objects:

In the template

 #{form @Controller.Action, method="POST" ... } <input type="hidden" name="object.id" value="${myobject.ID}" /> #{crud.form object:gun.gunEngraving} #{/crud.form} <p> <input type="submit" value="Save Changes" /> </p> #{/form} 

hidden input sets a special field "id" to:

In your .Action controller

 function static void Action(routeParams, MyModel object) { some validation; object.save(); render or renderTemplate or other action for redirect; } 

This, of course, is a simplified code, but I really like this template when I quickly need to embed a form in a view and cannot / do not want to use the entire CRUD system!

[Edit] More advanced custom CRUD function

The crud tags really don't need the crud module. So much so that I eventually copied them to my project, hacked them to add additional cool features, such as the ability to change the name of the object in the form from the default object (I decided to redefine the originals, but you can make your own just using another folder than tags/crud for namespace)

+10
source

Source: https://habr.com/ru/post/904782/


All Articles