How to customise naming of symfony 1.4 form elements - symfony-forms

I know this is possible but I can't seem to find the right search string to get the answer online.
I have MyForm()
and I want to create 5 of these rendered within the same form so I want to change the naming structure of the form elements thus:
name="forms[1][fieldname1]"
name="forms[1][fieldname2]"
name="forms[2][fieldname1]"
name="forms[2][fieldname2]"
etc
so that when it is submitted I can just iterate through the $_POST['forms'] array, binding each one to a MyForm instance and validating.
I'm sure you can customise he naming of fields for symfony forms in some global way rather than going through each widget but I just can't find it on the web.
Anyone point me in the right direction?
Thanks.

Solved own problem :
$this->widgetSchema->setNameFormat('my_form[][%s]');

Related

angular dart >> Best way to create a table:column renderer

I'm hopping this awesome community can steer me on the right direction. I came from the flash/flex/js world, and I like how simple it is to define an item renderer in flex. Here is what I'm trying to accomplish:
I have an Angular component which consist of a form and a html table. I have the columns, headers, rows, etc. all populating correctly using ng-repeat. I want to be able to define column "renderers", so if someone passes me a column property like "renderAs: 'button'" or "renderAs: 'progress'" I should be able to render the entire column as a button, or progress bar, etc.
Here is what I've tried so far:
ng-bind-html="getColRenderer(column.renderAs, column.value)" which
returns HTML based on 'renderAs'. As you all probably know, this
will only work with basic HTML stuff, but I cannot append an
'ng-click', or an 'href' due to angular's security. So, I opted
for something else.
I semi-have a good solution embedding a "ng-switch" inside my
ng-repeat. I had an ng-if but with several types of potential
"renderers" I opted for the switch. This somehow seems like future
problems while trying to display too many columns or rows, just my
fears.
Decorators - I like decorators, but it seemed a bit too much for
something as a simple button that calls something on click or a
progress bar with 2 values. So, I halted going into this path, but
if this is the shinning path all walk, then by all means.
I hope someone out there has ran/done something like this and can steer me on the right course of action. If the ng-switch or ng-if is okay, then I'm good to go.
Once again, thank you in advanced.

Reference ActiveRecord attributes in textarea

I am building a simple invoice application, and I would like to allow the users to customize the text on the invoice. In addition to this, they should be able to reference specific attributes in my models, i.e. "This is a test {{Model.attribute}}", and once the text is parsed the tag is replaced with the value of that attribute.
I have looked a bit at redcloth, textile and handlebars, but it does look like a little bit overkill to be honest. For instance I would not like to allow the users to input any HTML.
I would really appreciate if someone could point me in the right direction. There is probably a gem for this that I just havent found yet.
Thanks in advance
I use liquid with simpleformat which will sanitise the text.

Rails 3 - How to create custom html components that are treated with CRUD operations?

I'm using Rails 3 to create a project that will need a model called Sketch. I've already created a model, controller, and migration to handle Sketch - so far it just creates a 'sketch' object with a name for each sketch.
My problem is that I need to be able to attach an html5 canvas to each sketch object when it is created (or remove it when it is destroyed).
Since 'canvas' is not a datatype that will be stored in the database (like 'string', 'integer', or 'datetime'), how do I go about creating custom html components such as this that need to be treated like any other datatype in a Rails app?
I'm assuming that you would need to add the html components to a Model method and use a callback - like after_save - to initiate the component. But I'm not sure at all how to do this.
Not sure if I'm describing this well enough, so here is a very simple mockup:
I have the Raphael Javascript library in mind for the component that will do the sketching - if that helps.
If you can point me to any tutorials on this subject that would be great.
HTML5 canvases are rendered in the browser, not on the server where your ruby code is actually executed. Therefore I think it's safe to say that what you're asking isn't possible (at least in the way the question is phrased).
Instead you'll need to work with HTML, CSS and Javascript in your view to get the canvas working.
Canvas Tutorial / Reference
Hope this helps.
(On a related note, it's also considered a bad practice to mix view-related concepts in with your models.)

Fill a rails form with a hashmap

I have a difficult situation.
I let the the user create a form through a Rich Text Editor and then I save this.
So for example, I save this literally into my DB:
http://pastebin.com/DNdeetJp (how can you post HTML here? It gets interpreted, so now I use pastebin...)
On another page I wrap this in a form_tag and it gets presented as it should be.
What I want to do is save this as a template and save the answers as a hashmap to my DB.
This works well, but the problem is I want to recreate what checkbox/radiobutton/... is selected when the user goes back to the page. So I want to fill the form with the answers from the hashmap.
Is there a way to use a 'dummy' model or something else to accomplish this?
Thanks!
Since you're pasting in raw HTML which is not properly configured as a template, it is more difficult to enable the proper options based on whatever might be stored in your DB.
The reliable approach to making this work is to use Hpricot or Nokogiri to manipulate the bit of HTML you have and substitute values accordingly. This isn't too hard so long as you can define the elements in that form using a proper selector. For example, create a div with a unique id and operate on all input elements within it, comparing the name attribute with your properties. There may even be a library for this somewhere.
The second approach is to use JavaScript to enable the options in much the same fashion. This seems like a bit of a hack since the form itself will not have a proper default state.

Parsing blog post tags from a text_field

Ok, so you know how you ask a question here, and in the "Tags" field you can enter several space-separated tags into a single text field?
I'm trying to replicate similar behavior in my Rails app. Except instead of questions, I'm doing a blog app (which has "posts"), and tagging those.
I'm using "form_for" to build the quick form. Inside of that I have the line:
f.text_field :tags
The problem I'm running into is, "tags" is not a field on my Post class. My Post class HABTM tags. So, somehow I need to parse the tags text field (using String.split), and pass the resulting tag Strings into my controller, so my controller can create and associated the tags along with the new blog post.
Is using "form_for" not going to work in this case? Is doing this sort of behavior beyond the design of the quick-and-dirty "form_for" functionality?
Thanks!
Unless you really want to reinvent the wheel, I would suggest using a plugin for this. ActsAsTaggableOnSteroids is a mature one. http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids
Agree with Ben on this - there's lots of great plugins and features/helpers that make them simple to use. And you can learn a lot about how to do this in a well-designed way. Here's another good choice.
http://github.com/mbleigh/acts-as-taggable-on

Resources