Laravel 4 - get data from multiselect form - forms

I'm using Laravel 4, and I have two tables related 'Many to many': 'Actividad' and 'Material'. Every Actividad can have one or more Materials, and every Material can belong to one or more Actividad.
So, I have made a form to create a new Actividad where you can save one or more Materials. I've done that with a multiselect input. Like that:
{{ Form::label('material_id', 'Material necesario:') }}
<p>{{ Form::select('material_id', $material_id, Input::old('material_id'), array('multiple')) }}</p>
I don't know if I'm doing correctly but, before saving anything, my first problem is that I'm only obtaining one result. I suppose I should get every option 'checked' in the form... The relevant part of my Controller is:
$material = Input::get('material_id');
return var_dump($material);
I should obtain a list of options selected, but the result of that in my browser is:
string(1) "1"
This is the first time I'm working with tables related in this way, and probably I'm doing something wrong (in the form, in my controller, in my Models,...)
Thank you very much for your help!!

Just change your code to this
{{ Form::select('material_id[]', $material_id, Input::old('material_id'), array('multiple')) }}
I hope this helps.

if you are using custom response handlers on the client side such in the case of submitting info with AJAX, all you need to do is to simple add "[]" to the name of the select control.
e.g.
<select name="material_id[]" multiple>
This is the same case as with regular PHP. The other methods are required if you want Laravel to handle the form elements for you when rendering a new response/view. ( page reload ). This reload doesn't happen with REST requests.

Related

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.

Drupal 8 form and view in block

I am very new Drupal, In a block I would like to have a form with select box and submit button. Each options in the link is link of content page. That is when the user select an option and click the submit button it will redirect to other node page.
For the above requirement client used Web Form for Drupal 7 and I would like to clone the requirement for Drupal 8. I tried EForm and I am able to create a form but I can not able to show the form in Block Layout and View.
I am not sure the module Eform is suitable for my requirement.
Can you guys please help me what modules do I need to install for the above requirement in Drupal 8.
Okay, I don't know if there are any modules out there which do exactly what you want to, but you can build your own custom solution. In my eyes there are two main possibilities:
create a custom block type with a HTML body field, put your HTML in there and you're done. Advantage: easy to do, Disadvantage: hardcoded
clean way: create a new node type and/or a new category, which you will use for your country nodes. Then you'll create a block programmatically and query for all nodes of that certain country type or for nodes with the "country page"-category, whatever you use to organize those nodes. Then you just create a form out of that data and render it.
Advantage: dynamic, the select list will update itself whenever you add or delete new nodes of that type / category. Disadvantage: takes more effort initially
I personally would recommend using option 2. Option 1 is better for really simple and "stupid" requirements like showing some hardcoded text/image on several places on your site, or if it's something temporary like some campaign teaser, which will be over in 1 week and you'll throw it away after that.
EDIT:
Entity Query: https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A%3AentityQuery/8
How to build Forms in Drupal 8:
https://www.drupal.org/node/2117411
For display only your block in your templates with preprocess the best way is:
$block = \Drupal\block\Entity\Block::load('my_block_id');
$variables['My_region'] = \Drupal::entityManager()
->getViewBuilder('block')
->view($block);
And in your page.html.twig or node.html.twig or xxx.html.twig use your variable My_region like this :
{% if page.My_region %}
{{ page.My_region }}
{% endif %}
For details check:
https://drupal.stackexchange.com/questions/171686/how-can-i-programmatically-display-a-block

repopulate form data from an existing document

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

how to display particular fields in form Symfony 2.6

I need display particular fields in form in Symfony 2.6. I use a class Form. I have the folowing fields: name, email, message, send, recet. I need display all of them except recet .I try like this:
{{form_start(form)}}
{{ form_errors(form) }}
{{form_row(form.name)}}
{{form_row(form.email)}}
{{form_row(form.message)}}
{{form_end(form)}}
But, it's displaying all fields in form, it is not what I want. Even if i leave only {{form_start(form)}} and {{form_end(form)}} - its display all fields. Can someone help me wit this problem?
remove form_end, just close form with HTML
</form>
but then you must handle CSRF token generation by adding:
{{ form_widget(form._token) }}
{{ form_widget(form._token) }}
or try setting field you do not want to show using:
{% do form.recet.setRendered %}
but probably best way is not to add this field in the first place, rather than hiding it, by form options or event listeners depending on some criterias
A better solution is to remove the field from the form type. If you only remove it from the view, it may be interpreted as a blank submission for that field and delete existing data.
If you only use the form in one place, then just remove the field from the type. If you use it in multiple places, then you can selectively remove the field in a FormEvents::POST_SET_DATA event listener.

Generate subform in ajax call

I have a form with collection of subforms - student with different studies - relation manyToOne. I have corrent database schema and entities and form builder works well. I don't know how to append new "study" object. I need to get html tags from somewhere in either cases - when there is at least one "study: object (clone him) or there is no such a one.
Let's assume that study object has 2 fields: name and year. If for a student there is such a record (object) it's first input in generated form has name "student[study][0][name]". And is surrounded by . When I click "Add new study" button I want to duplicate this surrounding div and change id's and name's of html form elements respectively. Is there ready library or method to use?
But there may happen there is no study records so far. So I need to get form from server through ajax call. Unfortunately returned form has inputs with names like "study[name]". Is it possible to render this form similar to first case - I mean "student[study][0][name]". But i'd like to avoid manually generate twig template for form - I prefer
{{ form_widget(form) }}
You should be dealing with data-prototype rather than issuing separate AJAX request. The whole concept of adding/removing subform items is described here:
http://symfony.com/doc/current/reference/forms/types/collection.html#adding-and-removing-items
Obviously, you will need to some JS (jQuery is highly recommended) in order to replicate subform fields.
You should note, however, that data-prototype behaves differently when you initially have empty or non-empty collection. At least I have encountered this weird behavior. As far as I remember, in first your when you say {{ form_rest(form) }} additional DIV is appended with data-prototype attribute consisting of form's HTML. In second case actual HTML (not as an attribute) is appended with ID attribute "form_name_$$name$$" where you need to replace $$name$$ with proper index.
Now, you really should take a look - maybe all this has been fixed in some recent versions but I can't be sure...
Hope this helps a bit...