Laravel 4: Reusable Forms (Add / Edit page) - php

When we develop forms for CMS (for example an Add product page and a edit product page), we usually develop 2 pages. But this result in double work and harder in maintenance & amendment.
Is there any way that I can do a form to be reusable on both add / edit page with Laravel?
Thank you.

Yes, provided that your add/edit form is identical. Here's a brief idea on how it can be done:
Load the form using Form::model binding (http://laravel.com/docs/html#form-model-binding) which will populate the HTML fields with current values (for editing) or empty (for new form)
Add a hidden field in your form, like the product_id which is either loaded or generated depending on whether it is an edit/new form
Upon submission to your controllers you can use something like this:
//after form validation
$new = Product::firstorCreate(array(
'product_id' => Input::get('product_id')
));
//assign the rest of the fields
$new->save();
I hope that can give you an idea to solve your issue.

I use more obvious approach:
Former could be used both in views and controllers, so I use it in controller
I create simple private method that holds my form
Use it both in add and edit action

Related

SilverStripe form pagination

I am using a form on a SilverStripe page, the form is currently quite long. I am looking for a way where I can paginate the form. I would like to have 5 fields showing. Once them fields are filled out the user could then click next and be shown the next 5 fields. Also a previous button would be required. Thanks for your help if you need any more info let me know.
You probably want to look into the multi-step-form module : "MultiForm is a SilverStripe module, allowing flow control for forms, and step process to be automatically determined based on configuration variables on each step class. It augments the existing Form class in SilverStripe."
I believe this allows you to do what you need. The github repository has a complete example to set up and I recommend reading it in detail.

handling submission with a "Form Maker"-created form

I'm using Drupal 7 and HAVE to use the Form Maker (https://drupal.org/project/drupal-form) module. Since it's not a normal form, I don't know how to do something like hooking as Drupal form submission Q/A explains for normal forms.
The available options for "action after submission" for those in-database forms are:
Stay on form.
Link to an article.
Display custom text.
Redirect to an URL.
Is there any way I can handle the form submission? e.g.: Is there a way to reference the form state for the submitted form in the url I specify if using (e.g.) option 4? (or whatever mechanism exists to recover that data by knowing+referencing the submitted record).
BTW I'm a total drupal n00b, and couldnt Find TFM to R (so I can follow the RTM principle) about that special issue in the Form Maker doc.
It looks like this module still uses the form API, so you should just be able to use hook_form_alter() to modify these forms. I'm sure there's some kind of naming convention for the IDs of these forms, which will allow you to apply changes exclusively to form maker forms.
If you need to customize the handling of form submissions, you can include your own submit handler by using the #submit form attribute. Note: the example in the link adds the submit handler to a field in the form, but in most cases I add the submit handler to the form array itself. (i.e. $form['#submit'] = array("submit_callback"))
Edit in response to comments
You do not need to reference a form_id when invoking hook_form_alter(). Instead, you can invoke it globally, which will cause drupal to call your hook on every form. As you can see from the documentation link I provided, the $form_id is passed to the function, and you can use that to conditionally modify the form. Please look at the comments on the documentation page for usage examples.
Unless these forms are being embedded using a third party service, if this module is actually legitimate it will be hooking its forms through the form API. Implement hook_form_alter(), and print off the form_id's that are passed to the function. Go to a page that has one of this forms on it, and see what gets printed.
(Also, [form_id]_form_alter() is not the correct syntax for invoking this hook on specific forms. Should be hook_form_[form_id]_alter(), where hook is your module name.)

Adding custom field to a backend form

I am working on a symfony 1.4 project and am trying to add a custom field in a backend form. At the moment, my module has a batch history with editing and deleting options for each row and a form that allows the user to filter through it. I would like to add a custom creation form and I am struggling to do that.
Going on the /*model_name*/new/ page does show me a form: this is the form I would like to make changes to.
I would like to display a dropdown list of elements from another table (and model) on this page. How can I do that knowing that the current module's widgetSchema doesn't have that relation?
Should I edit generator.yml? When I try adding a title: to new: , it doesn't seem to affect the /new/ page (even after cache:clear).
It would be easier if you create that relation in your schema.yml. Your generated form will be able to render the widget you want.
Otherwise, what you need to do is edit the *model_name*Form class by adding the widget and validator you want (sfWidgetFormDoctrineChoice and sfValidatorDoctrineChoice in your case)
The generator.yml uses the form *model_name*Form class by default, but I suggest you copy this class in a lib folder you create in your module, and edit this one instead. Then, you edit your generator.yml to use this new form, this is how you do it
Also, make sure there is not much rows in the table of your relation. Else, symfony will try to generate a html select tag with a lot of options and you will need to kill your php and even webserver processes.
Yes, you need to edit generator.yml, but in the form line:
# apps/backend/modules/whatever/config/generator.yml
config:
...
form:
display:
Whatever...
Here you have some examples. Taken from the official documentation:
http://symfony.com/legacy/doc/jobeet/1_4/en/12?orm=Doctrine#chapter_12_form_views_configuration
This way, the form that the generator uses will be the one you specify. And maybe you don't need to modify something else.

Adding input fields to a cakephp form through admin

I am struggling to find out if I can do this but I have a cakephp form with many input fields and when submitted I write them to a text file I can add fields in dynamically here by clicking a button but:
My question is if I have an admin page how do I get that to dynamically add fields to the page and make them stay there forever not just for that session?
Really stuck figuring out how to do this any guidance would be much appreciated.
Thanks in advance.
Make them persistent. You need to store the form and the fields it has: "Form hasMany FormField". The fields should describe a type so that the FormHelper will render the correct inputs.
When you want to display the form read that information from the db again and render the form according to your stored information.
I am guessing you already have a javascript function that dynamically adds the field to your admin page. Is that correct?
I don't use CakePHP, but a general workaround I use is to use the javascript function to at the same time update a table in the database that stores the form layout.
Here are some Jquery Form builder plugins that you can use to achieve the goal.
http://www.berthou.com/us/2009/09/05/dynamic-forms-builder-with-jquery/
https://github.com/botskonet/jquery.formbuilder
http://code.google.com/p/jquery-form-builder-plugin/
http://www.elizaibeth.com/2010/11/23/dynamic-jquery-form-builders/
Here is an excellent link of PHP FormBuilder class with tutorial, that you can use as a vendor library into CakePHP.

zend get posted variables from static form from the view file

Ok so i tried using zend form but what i'm trying to accomplish is way too much for me to handle zend form. I'll try to describe it in a few lines maybe you have a solution for me if not you will understand why i chose to use a form in a view file.
I have a form for searching products in a database. THe search is done using autocomplete (custom made). When the user presses "Add product to list" the product is being added to a div in the form, creating the impression of a list. I want to submit this (the newly added inputs in the form) to the controller and process the form. I don't know how to do this, or it is not possible, have no clue yet but the zend form gave me so many headaches that i am very close to stop using it.
So i have designed a static form, in my view file. I have my jquery stuff there, i add data (hidden input fields and checkboxes) and i want to post to my controller. The question is how do i get the $_POST array in my controller?
I'll try to answer you as better as I can givin how your question is vague.
If you have a html form on your webpage all you need to do is set its action to your controller:
action="mycontroller/myaction"
And in case its not:
method="post"
And in your controller in fact this would work:
$_POST['param_name']
but the Zend way would be in your controller's action:
if ($this->_request->isPost()) {
$data = $this->_request->getPost();
Zend_Debug::dump($data);
}
Hope this help. If you need more details edit your question to make ti more clear.
Also it does'nt matter if the form was created with Zend_Form or by hand that code will work regardless.

Categories