CakePHP - Automatically populate form fields from model - forms

I thought the form helper did this already, but can the fields in my form automatically be populated with values from the model or do I have to set them all as template variables and set them all as the value of each field manually?
The form is for a user profile, so I just want it to put the user's names, email, etc in the correct form fields automatically.

for example: in controller $this->data = $this->Model->find('first',array(...));
in view:
$this->Form->create('Model');
$this->Form->input('field1');
...
$this->Form->end('Save');

Related

Change field required dynamically base on user select in Symfony 4 Form

i need my app require(true/false) a title(TextType) based on what select from 'event' field(ChoiceType) in a single form.
So, how can i modify my 'title' base on what user select on 'event' dynamically?
How to dynamically Generate Forms Based on user Data
https://symfony.com/doc/current/form/dynamic_form_modification.html#how-to-dynamically-generate-forms-based-on-user-data
Add the EventListener on your ChoiceType.

Symfony2.8 ChoiceType form filed with TextType form field

In Symfony 2.8 when I edit or create a user, I create the UserType form to render all fields.
The UserType form has an Roles field which I get all roles and render it like checkbox.
The question is our boss want to add an input field at every each roles on the right.
The red color I marked section is the needed input section
.
In this way our boss can edit the input text box so that he can see more clearly every role's meaning.
How can achieve this?
One solution is to move Roles to DB and store them as other entities. In such scenario you will need Role entity with at least two properties:
name
description

Generate custom Azure web form

I am working on Azure C# project. There is webrole with web form collecting customer’s information.
Depending on the information, they submit I need to create new custom web form for each one of them.
How can I generate new web form with custom web controls inside same site with code? I would prefer not create different site for each customer.
Any ideas appreciated!
You don't have to create a custom web form for each customer, you can have it all in on dynamic form and draw the input controls according to the customer.
You will have to have a form a form or a database table to configure the input needed for each customer which will be used in drawing the form.
ex:
You can have a table named: CustomerAttibute with the following columns
Customer ID
Attribute Name
Attribute Type (Number, text, boolean, ...)
Is Required
Validation Reg Expression
In the web form, you will read the customer ID and retrieve the attributes related to that customer, and use these attributes to draw the form, ex: an attribute of type text will be rendered as a textbox but an attribute of type boolean will be rendered as a checkbox
Capture the values from the user and insert it into another table, ex: CustomerAttributeValue which will have the attribute ID and attribute value, the value will be string to accommodate for any type

Multi page form data validation in Play Framework?

I am a beginner in java play framework. I an building an app which will take form data spread in multiple pages say 4 to 5 pages. But all the data is mapped to same model. How can I get the data uploaded by user per page, validate against my model's constraints, and at the end save whole data in my model.
For ex:- If page 1 has name field which is required, and page 2 has hobbies field which is required. Then how can i specifically validate data filled in that particular page, navigate till last page, and save all the data in model, in last page.
Model would have 60-70 fields.
I am using Hibernate ORM.
Thanks !
You could prefill the next form with the values of the last form, save them in the database, go to the next form, load the entered values from the values and pre-fill the next form and so on. For this, use a method in your controller:
public static Result fillOutForm1(){
Hobby form1 = new Hobby("sitting still", "diabetes");
Form<Hobby > preFilledForm = newHobbyForm.fill(form1);
return ok(views.html.YOURVIEWCLASS.render(preFilledForm);
}
With this you "send" some values to the first view class with a form. In there, the user answers some more fields and hits Submit.
So you need a route back to your controller, to handle the new input in routes.conf:
GET /Form1 controllers.Application.fillOutForm1()
POST /Form1 controllers.Application.sendForm1()
This is in your controller class:
public static Result sendForm1(){
Form<Hobby> boundHobby = newHobbyForm.bindFromRequest();
Hobby newHobby = boundHobby.get();
//In your hobby class you need a finder implementation, so that you can interact with the database. With save() you put it into the database. This may be different for the database you use!
newHobby.save();
return redirect(routes.Application.index());
}
You can then not redirect to the index, but to the next view class with the next form and there get your last answers from the db, prefill them again and put them into the form. Do this as often as you need.

Symfony: get new ID in a form

I have an object displaying within a form, a hidden field being related to its PK (field id).
When I create a new object, the field has a null value. Submitting the form, the object is inserted into the DB and it now has an ID, but the field in the page still has a null value.
If a reload the page, now the ID is indeed set in the hidden field.
In my opinion, this is due to the form processing of Symfony: when a create an object, it creates a form, with this form valid the object is saved but the form still uses the data before it was saved.
The question is: how to get the auto-incremented key in the form up-to-date? Shouldn't the form only have a reference to the object? Can't the value be updated?
make sur that you call $entityManager->flush() method after insert and that you bind your form whene you have same data in your request object
$form->submit($request->request->get($form->getName()));
You should have an Entity assigned to your Form by FormFactory. Then Symfony will fill that Entity with submitted values. What's left is only persist the Entity and flush to database.
You can find steb-by-step form submission in Symfony Cookbook