How to fill initial values for select on HTML form from database? - json

I'm new here. Can you help me with a newbie question, please? Maybe provide a link to solution.
I have a RESTful service and a web form for editing a model (e.g. user preferences). I want to prepare initial values on form. I request "new" model from the service and it sends me a JSON that contains object with initial values set (for example gender or birth date). It is easy to fill text inputs with those values. But also form has several select elements which should be filled with options from a database. And then I can set appropriate initial options (id) from the model for those select elements.
What is the way to do this?
I see couple ways:
Service might send options together with the model in JSON;
Form can request options itself before getting initial values (but it is another request and if form has several selects it is several requests).
Is there any conventional or best practice way?

Related

How to get default values on a form submitted via rest?

I'm surprised this hasn't been asked already. I have a form on a web page which is being submitted via a restful web service (it's actually using angular js). The question is how should I set default values on the form? (This is for creates as obviously on an edit I'll be getting values from the server anyway)
An example
Let's say we have a checkbox on the form to opt in/out of marketing emails.
I would like to receive marketing emails [x]
In this case the checkbox is defaulted to checked so they have to physically opt out. In the old world of generating html on the server I probably would have returned these defaults from the business layer and then generated the form html with the defaults set.
Solution 1
Set the default values in the client. As I'm using angular I could have a JS object that represents the default state of the form and use this to initialize the form on a create.
My problem with this approach is that it just feels a bit wrong to be putting business rules like this in the client.
Solution 2
Another option might be to hide fields that have default values and only let them be changed on an edit. This might be ok for some types of field but for our optin field they definitely need to review this. This could be done if the form initially displayed those fields without defaults, then when you click a button to continue the form is saved. The client could then do a get on the object just saved and display all fields with the now populated defaults.
I can't decide if I like this approach or not. It's more requests to the server so in that respect it's not very efficient. Depending on which fields have defaults it might look a bit odd suddenly being presented with some extra fields.
Solution 3
Finally we could create a rest web service to get the default state. I haven't seen any other examples of this so not sure of any conventions. Something like:
GET http://server/rest/myresource/default
Are there any conventions on this kind of thing? Something I've missed? Anyone done this themselves and found a way that works best?
Thanks
If you are talking about an editing form, you have some "defaults" (i.e. the already saved data) and the data that the user is inserting. So you basically get the data (via $http) and use it for extend the form fields (via angular.extend). Here is a fiddle which explains it better: http://jsfiddle.net/F9rD8/
After reading through your comments, I think the problem might be thinking of Angular framework as Presentation Layer. Yes - It is a client side framework, but when running on the client side, Angular provides the MVC Presentation Layer. The recommendation is to augment the presentation layer with Angular "Services".
You might want to have an Angular Business "Service" that provides the defaults for the new form.
In the Angular world, your Layered architecture is essentially on the client side with Views and Controllers forming the presentation layer and both client side angular services and Server side APIs forming the business layer.
Angular services - https://docs.angularjs.org/guide/services
Hope that makes sense.

Database and html DOM id mapping

I am working on a portal, where users can submit and retrieve data from a database via ajax and servlet responses.
My question is how should i not reveal the real ids of the database in the html DOM. To be more spesific, i need to know which is the best way for mapping between "real" and DOMS's ids, and if this should be happening on server or client side.
Thanks!
I think you would like to populate the doms with unique IDs to identify the element in the page. In that case there will be numerous ways.
easy one would be a Fancy value put in the dom like
dom_id = 'itu~#'+(your_id*1001)+'#rand()'
Then when requested you can retrieve your_id with the '#'separated and divide by 1001 .
OR
be more complex you may write any complex function
Happy coding.

Query MySQL Database Client Side

I am trying to validate that a username is unique on a registration form and would like to verify the uniqueness of the username right after the client types it as opposed to performing this server side after the form has been submitted.
Should I collect a resultSet from the database, store it in an array and then pass this along to the jsp page in the form of a bean (I am using a model 2 design so the user passes through a servlet before arriving at the jsp page)? What if the array is very large? How do I bring this data into javascript?
Alternatively, is there a way to do the query using ajax and javascript all on the client side? Maybe its possible to somehow run the query in the background?
I am really just looking for some direction because I am clueless as to what to even begin researching something like this. Is this even a smart move, performance wise?
I'd use "AJAX" for this.
Here's one approach: set up a blur() handler on the username text field of your form. When the blur() method is invoked, you post the username to the backend code; it verifies it and returns some appropriate response. You then parse the response and change the CSS class on the username text field (e.g., turning it red) -- or do whatever else visually you want to do to indicate "username in use."
Either way, you've got to get the username from the client to the server for verification; you wouldn't want any mechanism which allowed the client to directly use the DB (think security/exploits/etc).
If you're not already familiar, check out jQuery (http://jquery.com/) to make your client-side life much easier.

Populating grid and form with same json?

Is it possible to populate grid with some of the JSON data and a form with other, from the same JSON? Two stores or two models or both? simple example... ;-)
Yes, the best way to do this would be to manipulate the reader, which as well as returning records, also stores whatever the raw json sent from the server is.
The easiest solution would be to specify the reader parameters for your grid, but then have a listener on the store, which then processed the rawData property from the reader to get the additional values for your form.
Of course if your form data is related to the grid data, you may do better to rely on nested loading and form.loadRecord in the store's load event handler. See the Ext samples (form and grid data binding example) for wasy of doing this.
Depending on the circumstances, another approach similar to the Ext FAQ would be to handle the Ajax through a simple Ext.Ajax.Request, and this process the json through two stores with local proxies, but this doesn't seem quite so Ext4 data model friendly to me.

Django: Storing values into MySql from Templates

I have used a checkbox in my html template, On submit, i want to store these Boolean values into the MySql Table. Please advice me on, How should i code my views such that it accepts values from Templates and stores it to the database. Also suggest , if any alternative method is possible.
In order to store data into models from the user input, django provides a very good form API called ModelForm.
Documentation: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/ clearly illustrates how to do it.
Essentially, you create a form and tell it, the model (which is your mysql table) you need to save it's data to and its all done for you. After validating.
In addition to becomingGuru's answer I think you have to learn how data is transported via HTTP and HTML forms work, i.e. how to process user submitted data at the server side. This is not Django related.
But in Django you can access the data submitted in the views via
request.POST['parameter_name'] # for POST data
# or
request.GET['parameter_name'] # for GET data
# or
request.REQUEST['parameter_name'] # combined list these parameters
then you can store them in your database.