Advanced customization of CRUD forms and controllers in Play

前端 未结 1 1608
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 22:42

What I\'m looking for is the ability to quickly (DRY!) generate forms for given models, but in a less-controlled way than using CRUD forms/models; for instance, by being abl

相关标签:
1条回答
  • 2020-12-09 23:21

    I found in this play! google group thread the answer I was looking for.. everything is already there, though undocumented!

    It's as easy as using:

    to display a creation form of the Model class.

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

    to display the edition form of any existing instance

    #{curd.form object:anyInstance /} 
    

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

    In your 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}
    

    the hidden input sets the special "id" field so that:

    In your Controller.Action

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

    This is of course a simplified code, but I really like this pattern when I quickly need to embed a form into a view and can't/don't want to use the entire CRUD system!

    [Edit] More advanced custom CRUD goodness

    The crud tags actually don't need the crud module. So much that I ended up copying them all over to my project, hacking 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 override the originals, but you can make your own just using a different folder than tags/crud for namespacing)

    0 讨论(0)
提交回复
热议问题