symfony 1.4: For forms, in an action, removing fields - symfony-1.4

I have my form in my action:
$this->form = new SomeForm($this->data);
the form has a bunch of fields I don't need for one action but has stuff I need for another action. What is the best way to handle this? Create an individual form for each necessity or dynamically remove fields when I instantiate it as above?
Thanks

It sounds like you are doing a multi-part form.
I assume that you want to validate all the values submitted in your form, but just not save them.
Its sounds like you are doing an abstract form, so don't extend a base object form, rather extend BaseForm.
Don't unset the values, use the form to validate them, even if they're going to be used later.
They are saved to the form object, so you can use that to pass values to your next action, so this is helpful, plus they are validated.
Override doSave() in the form to save the objects of the form you want to save.

In my opinion for this case, extending the form to each need and applying, through the override of the setup, selectively the unset instruction, you get a code a little more readable and maintainable.

Related

How to get the form name in class?

I have a form which is used for automatic journal postings.
On that form I have a Ok command button and in closeOk method of the form I call the method from my datasource table.
In the JournalCheckPost class's infoResult() method I want to determine if the method is called from my form. I know that it can be done with caller methods but I don't know how exactly it should be done technically.
It is bad practice to make a method depend on where it is called from.
What you can do is to pass an extra parameter to the LedgerJournalCheckPost and infoResult can then check that. This can be done by introducing a boolean flag and a parm method.
I think, there can be many situations:
You want to pass some parameters from form
You want to manipulate the form (for example refresh datasource after action is complete)
Something other
But in all the cases depending on particular form is not a very good idea.
In first case you can set parameters from code using parm methods, or, better pass parameters using the Args class
In the second you can cast Args.caller to some interface that contain all the methods you want and manipulate the form using that methods (see \Classes\SysFormRun_doRe usages for example)

Add to Form Results from External Form

Is there an API for adding to the Form Results that results from standard Forms are added to from an External Form?
I want to try avoid adding to the tables btform, btformanswers, etc. manually
No.
See https://github.com/concrete5/concrete5/blob/master/web/concrete/core/controllers/blocks/form.php#L354-L415 -- the core's form block updates the table manually.
As johjoh says, you could theoretically mimic a post to a form block, by instantiating it and then calling action_submit_form(), but that's just as fraught with difficulty, too... you'd have to keep the "form" in sync with your data, and possibly worry about the token and block ID and all that....
What's your exact use case? New block type? Some sort of external API? The form viewing interface in the dashboard is nice, but nothing that special. I think most people want to get data out of it, not in....

Override methods in dynamic form

Is there any way to override method in dynamic form?
I've created a form from code (create Form, adding DataSource, etc. and then FormRun). The problem is with the datasource validation. In normal form (in the AOT) I'd use return true in validateWrite to prevent normal validation on table.
How I can achieve this only from code? (or more precisely: when I've only class to play with)
I think the FormBuild.addMethod is what you are looking for. Provide the FormBuildDatasource object as the third argument to the addMethod method.

Drupal save a node with required fields not complete

I have a content type which has a lot of fields 50+. 30 or so are required fields. I want my user to be able so save the node before all the required fields have been filled in. The node can not be published until all the required fields have been filled in. Is there a way I can do this.
Not directly. Required fields are exactly that. You'll need to make your fields optional and handle the node save event and prevent publishing until each of the fields has been filled in. If you don't feel like all that php, Rules module can handle this kind of thing very nicely - http://drupal.org/project/rules.
Another alternative is that you might be better off with something like a webform - http://drupal.org/project/webform, though I don't know if it has any save and resume functionality out of the box so you might need to check that out first.
This module will do the job:
http://drupal.org/project/multistep
You can split your form to steps and only the fields in the current step have to be filled by the user.

Create multiple CCK nodes with single custom form in Drupal

I need a form which will allow creation of several related nodes at the same time. All of the nodes involve CCK fields.
I would like to use as much of CCK's built-in validation, submission, input widget, and security functionality as possible/practical.
What is the best way to accomplish this in Drupal 6? Are there 'best practices' or docs anywhere?
Here are 3 possibilities I can see. I would love feedback on whether any of these would work, or if there are even better options.
1.
start with the standard node creation form for content type foo.
modify the form by adding fields for content type bar, using hook form_alter [can cck widgets for content type bar be inserted directly?]
use a custom submit handler to create node of type bar when the form is submitted
[can the standard cck handler be called? or do i need to 'manually' construct the node object, do my own validation, and use node_save?]
2.
create a new, custom form that concatenates the 'normal' node creation forms for the relevant content types.
then use hook form_alter to modify the forms as necessary.
allow standard cck submit handlers to do the work of creating the nodes.
3.
create a custom form from scratch
create the nodes in my own submit handlers, using node prepare, node save, etc.
If found documentation on re-using the standard node creation form, but creating multiple nodes at the same time is not mentioned.
Using hook nodeapi and hook form_alter is documented in a post on advomatic's site, but the particular method descrube seems to require polluting one of the content types with 'dummy' fields.
Thank you very much for your help!
The advomatic guys posted a nice solution to this.
http://www.advomatic.com/blogs/jonathan-delaigle/multiple-nodes-single-node-submission
why not just use hook_nodeapi to handle the node creation for certain content types.
just set up a test condition to see if $node->type = 'foo', and then run a function to create two nodes or however many, using the values from the predefined fields. you can even set hook_nodeapi to only run when the $op is almost ready to insert the node into the database, thus ensuring the object has been run through appropriate validation before being passed on to the new nodes that need to be created.
http://api.drupal.org/api/function/hook_nodeapi/6 this page has a list of all available operations for the $op variable and what they do.
I hope that helps
If the 2nd type bar needs only one or two additional inputs (fields) from the user, I would go with your approach one.
But given your clarification it seems that foo and bar are sufficiently different and complex, so your approach two seems more reasonable.
Concatenate both forms into one and hide the bar fields that you want to populate from the foo fields (or node, after you created it). In the forms validate and submit functions, you'll have to separate the forms again so that you can call the standard validation/submit handlers for both separately.
I have not done this yet, so I'm not sure how well this will play with the cck functionality, but I would expect it to work reasonably well to give it a try.

Resources