Fork me on GitHub

form

An article by Gaspard Bucher

Announce a form which will be used for ajax calls.

Announce a form which will be used for ajax calls.

  • publish Set whether to auto-publish node after save.
  • klass set the class of newly created nodes
  • mode redirect page mode
  • update dom_id to update after operation
  • preview dom_id to reload on form change
  • t_id id of node used to render the updated dom_id’s ajax template with (e.g. “main.id”). Defaults to the newly created node ID.
see add, drop, each, name, new, publish

If you do not specify <form> tags within the do=’form’ rendering block, it will be added wrapping the content. But there are cases where you definitely need both: declare the whole r:form and place the actual form elements. For example, to keep all form elements within the <td> in tables:

<table do='mates'>
  <tr do='each'><td do='link'/></tr>
  <tr do='form'><td><form>...</form></td></tr>
  <tr do='add'><td><r:add_btn/></td></tr>
</table>

It seems do=’form’ in the example needs to be moved into the <td> tag, for saving to work. But this still removes the preceeding <tr> tag (placing the <form> tag directly in <table>, so the browser marks it red when viewing the html.)

automatic fields

Whether you use forms in ajax contexts or to update/create new elements without javascript, the necessary fields to create the correct objects and/or to render the proper partials are automatically inserted.

For example, the parent_id field with a value > 0 is required to create a Node. When no input or select form element is defined for this field, an element is added that defaults the parent_id to the node context before new.

However, a title is also required to create a new node, this field is not added automatically.

redirection

By default, submitting a form will redirect the visitor to the newly created node’s page.

If you want the form to redirect to another URL after create/update, set the “redir” parameter.

If the submitted form contains the (hidden) redir parameter, it is used to redirect the whole page after object creation. You can use ”/NODE_ID” to redirect to the newly created object’s page.

<r:form>...
  <input type='hidden' name='redir' value='#{zen_path(main, :mode => 'foo')}'/>
</r:form>

You can also use the “mode” parameter to redirect to the node’s page with the given mode.

preview

To preview content as it is typed, add the preview attribute to <r:form> to specify the block for the live updates. In the target block, call preview_node, this applies all the params[:node] values from the form to the current context node properties for the preview. (The klass must match, else you may insert for example do=’Correctclass.new’, to enter the context of a new node of the Correctclass.)

<r:form preview='fprev'>
  <input name='foo'/>
  <r:textarea name='text'/>
</r:form>
...
<div id='fprev' do='block' do='preview_node'>
  <r:zazen eval='params[:foo]'/> <!-- Uses raw parameter -->
  <r:zazen attr='text'/>         <!-- Uses applied property -->
</div>