repopulate form data from an existing document - forms

I am trying to repopulate fields on a HTML form with a users data they initially filled out when they created an account for the first time in Meteor to allow them to edit information. The form contains various inputs, option lists and check boxes. The data stores on in the collection with no issues. I have a field in the user profile called profComp which is either true of false dependent on if the form has been filled out before. I have tried to add an if/else statement on the route -
Router.route("/edit_dashboard", function(){
if(Meteor.user().profile.profComp)
{
this.render("change_edit_dashboard");
}
else
{
this.render('edit_dashboard');
}
The change_edit_dashboard is a copy of the html form the user initially filled out and I have added a helper to the change_edit_dashboard template to retrieve the users info document from the collection.
Template.change_edit_dashboard.helpers({
formData: function()
{
return Base.findOne({_id: Session.get('baseId')})
}
});
And attempted to call context of document using spacebars {{#with}} in change_edit_dashboard
<template name="change_edit_dashboard">
{{#with formData}}
----> form code <------
{{/with}}
</template>
However a few strange things seem to be happening -
The change_edit_dahsboard template is not rendering at all. The formData helper doesn't seem to be getting called as I am not getting a test console.log() I wrote showing in the console.
With the addition of this code it seems to breaking all functionality of other templates. I have placed test console.log() in various template events and they do not seem to be getting called
Can anyone advise on where I may be going wrong? Thanks

As you did not give us your form code, I'll just assume you correctly gave <input name="myInput" value=myValueHelper> attributes to your inputs and go on.
From what I see, you are trying to get data from one of the most error-prone objects in Meteor, Meteor.users when one wants to retrieve data from a collection object. You can find below some possibilities regarding your issue:
Meteor.user().profile.profComp does not exist. As you are manipulating Meteor.users collection, you have to take a few extra-step in order to access data stored there. For example, if I have a mySuperField field at the root of my Meteor.users model, I have to publish it server-side in order to subscribe and retrieve it on the client. You can quickly check it by calling Meteor.user() and see wether it's present or not.
If you have a blank screen with no error server-side, it's often because you broke your templating load. Check error messages at the top of your web-console and make sure you did not put a template helper in a file executed on the server or before your .html file was loaded by Meteor.
I'm not exactly sure why you need 2 edit templates but if it's just a matter of new vs edit, you should consider using autoform with simple-schema as it will greatly help you through your forms. You can find the links below.
https://github.com/aldeed/meteor-simple-schema
https://github.com/aldeed/meteor-autoform

Related

Data in array collection shows empty when retrieving info

I'm creating a web app with symfony. I'm currently building the forms and as I've never used the ManyToMany relation I'm having some problem with retrieving the information.
The form I'm working with is this one:
The point is that when I'm retrieving the information of the array in the twig template, the data property inside the array shows empty, when there is Alumne's entities created. Let me show you.
Twig template (create page):
As you can see, what I pretend is to retrieve the info of every Alumne entity inside the array.
What I get doing this is:
The alumnes field is completly empty. But if I change the form Builder to this:
(I have also changed a little bit the twig template to make it more readable)
It works!
And as you can see there is an Alumne created.
The point is, as you can see, the select and option tag it creates is kinda ugly, I want to custom so it can fit the rest of the forms. So that's why I need a CollectionType in the builder and I don't know what I'm doing wrong. Also if I check the dump(form.alumnes.vars.data) it shows empty.
The issue here is, that you use the CollectionType instead of the EntityType. To load data from the Database into a form, you should use the EntityType https://symfony.com/doc/current/reference/forms/types/entity.html
If you leave the type out, Symfony will try to guess what form element to use and will (correctly) guess the EntityType

redux-form Wizard form with linked fields

I am building a multi-step application form with React. Having first built it with pure internal state I am now in the process of refactoring to Redux using redux-form.
Having used the example here as a basis: http://redux-form.com/5.2.5/#/examples/wizard?_k=oftw7a we have come a good way.
However the problem appears when i have two forms which are supposed to have the same value. During one of the pages i have a name field, that is supposed to be duplicated on the name field of the next page. The opposite should happen if you go back from the last page. Any tips to how this could be achieved?
Using the wizard, you are basically working with the exact same form that's split into multiple pieces. Ultimately it's the same form, because redux-form tracks them by name. It is how the library identifies the pieces of the same form - using the name.
form: 'wizard',
Here you can see that the exact same instance of the form will be shared throughout the pieces. fields work in a similar manner. Each field is defined as part of a form.
As long as you use the same field constants inside the fields object that you pass into the reduxForm function and as long as the value for form is the same, so that they use the same underlying form object, it should work for you just fine.
On one page you should pass in
export default reduxForm({
form: 'wizard',
fields : {
'fieldIWantOnBothPartsOfTheForm',
'someOtherFieldThatShouldOnlyBeHere',
},
...
And then on the other page:
export default reduxForm({
form: 'wizard',
fields : {
'fieldIWantOnBothPartsOfTheForm',
'thirdFieldHere',
},
...
Also, make sure you keep destroyOnUnmount equal to false if you want to navigate back-and-forth.
Hope that helps.

cfinsert is picking up my search input field in a different form

When I submit my form I am getting an error from my cfinsert function because there is not a database column name "SEARCHFIELD". The problem is "SEARCHFIELD" is not an input in the form I am submitting.
Both forms have close and open tags so I am not sure why my search form input is being referenced in my main forms submission?
Any thoughts?
Two ways I can think of to avoid this, without seeing your actual code it is hard to guess where SEARCHFIELD is coming from. As some of the comments pointed out it would most likely be from a CFPARAM or the name of your submit button in the form.
The first way you could tackle this is the CFINSERT tag has an attribute named formfields where you can list off the columns you wish to insert with. You can see that here in this doc link:
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c78.html
Another way you could do this is you could add code to remove SEARCHFIELD from the FORM scope prior to running the CFINSERT. Which would be as simple as:
<cfset StructDelete(FORM, "SEARCHFIELD") />
You could check to see if it exists and if so then delete it but the StructDelete() will run without issues even if the field does not exist.
I personally do not use CFINSERT/CFUPDATE and I know the default opinion in the community is not to. They do have some benefits though that often are overlooked in that they do parameterise the SQL and offer at least some safety from malicious people. Without knowing anything about what you are doing it is hard to say if using them is actually a shot in your foot in the long run or something just fine to be doing.

Symfony2: Entity instantiation upon Form-Submit depending on user selection

I'm working with Symfony2 to set up a form, where a Shelf-Entity can be edited.
A shelf contains a collection of Readable-Entities (e.g. Book, Magazine, etc. - all inherit from Readable).
The user has the possibility to add more Readable-Entities (the form is extended via JavaScript) and from a dropdown he can select the type of Readable he wants to add. Depending on the selected dropdown-value, different form fields are rendered. So far so good.
Now, when the form is submitted to the server, depending on the Readable-Type the user selected in the form, a different entity-type should be instantiated.
If I don't do anything, Symfony just instantiates the base class Readable (and not Book, Magazine, etc.).
How can I tell Symfony to instantiate the correct type of Readable depending on the selected value from the dropdown?
I tried with FormEvent-Listeners, but:
in PRE_SUBMIT I only get an array containing the "raw" form data with $event->getData(), i.e. no entities have been instatiated so far. However, at this stage, I still have access to value of the dropdown.
in SUBMIT the form data was already assigned to the appropriate entities. Also the new Readable was already instatiated with the base Readable-Class. But now, I cannot access anymore the value from the dropdown.
What is the correct way to do this?
EDIT
Added a minimal Code-Example for the Shelf FormType:
https://gist.github.com/anonymous/401495b701982adafb96
Code for infinite_form_polycollection:
https://gist.github.com/anonymous/b5f0ed10ca9c52177f01
Have you tried looking at this part of the doc? As "embedding a form" seems to fit your needs.
It seems that there was something wrong with the PHP-Files of the PolyCollection in the vendor-directory, because after removing everything related to the Infinite Form Bundle from the vendor-dir and reinstalling it with composer, everything is working now. But thanks for your efforts YoannCh

Zend Framework Dynamically added fields of a form and populate

I have been attempting to create a form where a user can simply press a button and the form will add a new field for the user to use. I have 2 of these dynamically added field types.
Firstly a field where a user can upload files, by pressing the add button another field is pasted underneath the current field and is ready for use.
I have followed an old guide on how to get this done with a bit of ajax and jQuery.
This guide to be exact: http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/
As you can see it's from 2009 and a bit outdated yet it still works under the current Zend Framework version 1.11.11
The problem however arises now that i want an edit / update version of the form. I need to populate it's fields but first of all i need to create enough fields for the data to be stored in. So when there's 3 files that have been uploaded it should create 2 additional fields and place the 3 file names in these fields ready to be edited and updated. Simply using $form->populate($stuff) is not going to work
I just have no idea how to accomplish this and the tutorial on dynamically added fields only goes as far as the addAction and not how to create the editAction under these conditions.
Is there any tutorial out there on how to create and manage forms such as these? I'm sure i am not the only one who's had the idea to builds these kind of forms?
I can add my code if there's a request for it but it's the same as the example from the guide, just a different set of elements in the form.
Adding a small example of it's use.
A user adds an item with 3 files, these files are uploaded along with a filename so in the database it appears like this : File_Id : '1' , File_Name : 'SomeFile' , File_location : 'somewhere/on/my/pc/SomeFile.txt'.
Now the user realizes he forgot a file or wants to delete a file from that list, he goes to the edit page and here i want the form to display the previously added filenames. So if there's 3 files it shows 3 and when there's 2 it shows 2 etc. How do i build a form to dynamically add fields based on the number of uploaded files and then populate them?
Any advice on how to handle this is well appreciated :)
You can make use of the semi-magic setXxx() methods of the form.
Inside the form:
public function setFiles($files) {
foreach ($files as $file) {
$this->addElement(/* add a file element */);
//do other stuff, like decorators to show the file name, etc.
}
}
In your controller:
$files = $model->getFiles();
$form = new Form_EditFiles(array('files' => $files));
By passing an array with key files you will make the form try to call the method named setFiles(), which you have conveniently provided above.
This should push you in the right direction, or so I hope at least.
If I understand you correctly you want to populate file upload fields, which is not possible because of security reasons.
Edit:
You can add Elements inside of the Controller via $form->addElement() (basicly just like the $this->addElement() statements in the Tutorial)