Symfony Set Drop Down Value Based Upon ID - php

I am still getting my hands dirty with Symfony, so I am a bit ignorant on how to do something that should be pretty simple.
I have a form that I am creating in the controller and passing to the view:
$form = $this->createForm(new PurchaseOrderType($account), $purchaseOrder);
The form displays exactly how I need it to, no problems at all! I am trying to now make the form more dynamic so that it can auto select a drop down list based upon an "id" variable that I am passing into the form. The id equals 23 by the way.
So, I have a drop down of suppliers and one of the options value is 23. How do I automatically select this option? Sorry for my ignorance :)
Thanks!

Without the code of your form type, I can make only suggestion.
If I get it correctly, inside the purchase order entity, there is an other entity mapped, and that one is represented with an id.
The object, $purchaseOrder has to have the other entity, then in the form type, when you set up the drop down field, you have to specify - with the correct name it shouldn't be a problem - the foreign id.
But you need of course data for the drop down field what can be the result of a SELECT * query.

Related

Symfony 3 - Form builder: Storing select field in database with doctrine

I'm new to Symfony and I would like your help in deciding the best way to design a form and store into database using Doctrine. Here is my task:
I have a form created with the FormBuilder containing various types of input fields. I have created entity which connects my database to form and it works both ways, both to fetch data and to put data into database.
However, I want to add a Select multiple field to a form which would be populated from a table from the database. And when I select the choices from that select element, I would like to save them into a database in either csv of id's or some simple serialized object or array.
So far, what I have managed to do is to add that Select field as EntityType and have placed ORM Annotation in entity for that element: Type("object"), which populates the Select element flawlessly. From the $_REQUEST variable I can see that it returns the id(s) of selected options. But, what I get when I submit the form is a very large serialized object in the field which is supposed to store selected element.
I suppose that it is a normal behavior (to store the entire object (an entire row of data)), but is there some cleaner, leaner way of storing only the id's that I would be able to later on use to populate the same select field, also using doctrine (since I don't have a need for other data in that field except for their IDs)?
Thank you in advance.

Create related record form on separate page with CakePHP

I have this model :
School hasMany Teachers
I have a view action/page on my School controller, where I need to put a "add new teacher for this school" link. This link must lead to the Teacher add page, and the new teacher must reference the school from the previous page.
I am doing this by adding a query string on my link, like :
Add new teacher for this school
Then in my TeachersController I pass this value to my view (teacher::add.ctp) as $school_id and create a hidden input in the add.ctp like :
$this->hidden('school_id', ['value' => $school_id])
So when I submit the teacher add form, its school_id field is set correctly.
Is there a better way to do this ? I am not really happy with my solution and the query string trick (one could change this in the address bar...).
Thanks
I guess you'll have to pass the information to the action in some way.
Instead of a link you can use a form (cake FormHelper has a formlink() method for this) and pass school_id via POST
Anyway even if you use POST data there are still many ways a user could modify the data.
The problem I see here is that you use an hidden field. I think it's redundant because when you send your data to /teachers/add?school_id=4you already have this information in your query data
so you can do
$teacher = $this->Teachers->newEntity()
$teacher = $this->Teachers->patchEntity($teacher , $this->request->data);
$teacher->school_id = $this->request->query('teacher_id');

Text field or dropdown list in yii

I have two tables:supa and acts. Supa table has ID as primary key and and Acts table has supa_ID as a foreign key that is connecting this tables.
Now in table Supa I has field that is called "oznaka".
I suppose to create text field where I will put "oznaka", but in background it is suppose to take ID of Supa table, connect with table Acts, find supa_ID and put him there.
I have troubles with controller function - I suppose I use view - controller only. This doesn't suppose to be hard but I have really trouble :(
<?php echo $form->dropDownList($model,'item_type_id', CHtml::listData(ItemType::model()->findAll(), 'id', 'type'), array('empty'=>'select Type')); ?>
This is basic usage in Yii to create dropdown list, populated from database.
If You want text box, You can make ajax call after some input is received to check whether in database is something that corresponds to this input. If there is, add new value to some hidden input field and get that data when form is sumbitted, in controller.

Display data from dropdown in another div

I have a dropdown with a list of values that are pulled from a MySQL database. Each of these values has other corresponding attributes in the database. This is the structure of that table:
id | name | password
The dropdown values are basically just all of the values in the "name" column.
What I'd like to do is display the id/name/password for a selected dropdown value in a separate div. That is, if I select "MIT" from the dropdown, I'd like the div to show me the id and the password associated with MIT. If I select "Harvard", I'd like the div to show the id and password associate with Harvard.
I am just looking for high-level suggestions on how I should approach a setup like this.
I was thinking of using AJAX and passing the selected value into a separate PHP file, which would then pull and display the associated ID and password. The div would then contain code to make a call to that PHP file and display the values on that page.
I think it'd work in theory, but it seems a bit cumbersome...any ideas for simplifying the process?
You could use data-attributes on your options. This would expose the passwords through the markup, but since they are accesible anyways through choosing the option in the dropdown, I guess this isn't an issue.
If you gave each option an attribute data-password="thepassword", then you could $("select").find(":selected").data("password") to retrieve the password. Do the same with a data-id attribute, and you're golden.
Another solution would be to have a JSon structure with the option values as keys. Let's say {"MIT":"1234","Harvard":"verysecret"} and then use $("select").change() and $(this).val() to retrieve the corresponding passwords (and other data) from you JSon structure.

Store dynamic form fields in database

I have read other answers on this (or at least near to this) subject but I couldn't get a clear view of it so I'm asking for help again.
I have a complex dynamic HTML form that I would like to submit to database using PHP. The form is split into multiple tabs and in each tab I got checkboxes that trigger other parts of the form. Example: at a point in my form I got a checkbox group that has options of: "hotel" and "restaurant". If I check hotels, I get another part of the form displayed, specific for "hotels". Same thing for "restaurant". So it's very dynamic here and I don't know which would be the best approach for storing every form field in database. Because it could contain 15 fields or 20, depending on the selection. Any example would be appreciated as I'm not that advanced with database design.
Thank you!
So it's very dynamic here and I don't
know which would be the best approach
for storing every form field in
database.
I apologise if I have misunderstood you here but I believe that you should design the database according to the data and not the form. It is difficult to comment without knowing the exact details of your situation so here is an example:
If you usually dump all the data from a form into a single table, but because sometimes this will involve submitting 5 values and other times this will involve submitting 10 and so you are unsure how many columns your table should have, then I think the problem is in the database design.
Work out what pieces of data are dependent on other pieces of data. For example, you mention checking "hotel" might open up more fields specific to that choice. Let's assume this involves things like "en-suite", "bed type" etc. Then you should have 3 tables, a registration table (assuming the user is using the form to buy these services), a hotel table and a registration_hotel table. The registration table will record a number of details specific to the registration only such as the customer's name and a unique id number. The hotel table will hold information specific to the hotel only, such as how many rooms have en-suite. The registration_hotel table will hold details specific to that registration at that hotel. You might want a column of type bool to record whether the user requested "en-suite".
When submitting the form, check which pieces the user entered with if(isset($_POST['hotel']) && !empty($_POST['hotel'])). Then only send stuff to the registration_hotel table if that condition is true.
If this design results in making too many separate calls to the database, you might want to look into transactions which will help you to manage the speed and security of these calls.
If you can post in a specific example of something you don't know how to do, that would be useful.
You didn't specify how you can manage this dynamic form. Can you edit it's PHP/HTML source? One great thing would be if you can label your different variables like hotel[], restaurant[], etc.
If your submitted form is clear enough (i mean semantically correctly structured) you can store the whole submitted form serialized.
Note: this method only working when you don't need to search for specific items in your database.
Edit: maybe i'm misunderstood your problem.
You can create a 'metadata' table like this:
form_id | option_name | option_value
---------------------------------------
1 | hotel | true
1 | restaurant | false

Categories