In knockout.js I have a data-bind as follows
<input id="lbl" type="text" data-bind="value: $data.quantity" />
here I would like to have a condition like if $data.quantity is 0 i need to bind with empty text so can some one help me
Just use a ternary expression:
<input id="lbl" type="text" data-bind="value: quantity() ? quantity() : ''" />
The above assumes that your quantity property is an observable, if it's not:
<input id="lbl" type="text" data-bind="value: quantity ? quantity : ''" />
See Fiddle
Related
I have these inputs in my twig file :
<input type="text" name="txtNom" id="txtNom" value="{{user.nom}}" />
<input type="text" name="txtPrenom" id="txtPrenom" value="{{user.prenom}}" />
<input type="radio" name="rbSexe" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe" id="rbFemme" onclick="changeGender(this.id);" />
So, for calling those inputs in my Controller, I use the name attribute, for the first two it's okay :
$utilisateur->setNom($request->get('txtNom'));
$utilisateur->setPrenom($request->get('txtPrenom'));
but those with radio type have the same name, so how can I call specific one of them?
$utilisateur->setSexe(?????????);
I solved the problem :
I give the inputs a value, and make the name looks like an array:
<input type="radio" name="rbSexe[]" value="Homme" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe[]" value="Femme" id="rbFemme" onclick="changeGender(this.id);" />
and for call it in Controller, I use this :
$s = $request->get('rbSexe',0)[0];
Let's say I have this following HTML code -
<input type="text" placeholder="name" />
Which I want to convert to
<input type="{{type}}" placeholder="{{text}}" />
How can I do this using Handlebars.js?
I'm using cmfformcontroller in an app to manage a list of entries.
Entries are displayed using :records as documented at http://pypi.python.org/pypi/zope.httpform
<form action=".">
<p>Please, enter information about one or more of your next of
kin.</p>
<p>
First Name <input type="text" name="people.fname:records" />
Last Name <input type="text" name="people.lname:records" />
</p>
<p>
First Name <input type="text" name="people.fname:records" />
Last Name <input type="text" name="people.lname:records" />
</p>
<p>
First Name <input type="text" name="people.fname:records" />
Last Name <input type="text" name="people.lname:records" />
</p>
<input type="submit" />
</form>
I want my validator to be able to highlight a record using state.setError method. How could I achieve this ?
I have fix this use case by keeping a list of error key pattern: on starting by a pattern: id_field
I have a check out form where number of products can be "n". So how i can know how many input fields are in the form and take input from it?
Thanks
If it's a group of single controls - say a variable number of checkboxes representing items - the solution is pretty straightforward. For your checkboxes:
<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="checkbox" name="ProductID" value="3" />Product #3
Then in your ASP, you could do this:
<%
Dim intID
For Each intID In Request.Form("ProductID")
' intID now represents a selected product ID. Insert into DB
' or whatever your process is. Note that only the "checked" values
' will be passed to the server.
Next
%>
In fact, this approach will work for any number of controls with the same name. If it were 1 - n number of textboxes with the name "FavoriteColor", you could For Each through each value in the same manner. Textboxes with no user input would not be passed.
Now, if your checkout form contains a group of input controls per item, you can build on that approach by carefully naming your other controls:
<div>
<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="textbox" name="Product1_Quantity">
<input type="textbox" name="Product1_Color">
</div>
<div>
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="textbox" name="Product2_Quantity">
<input type="textbox" name="Product2_Color">
</div>
<div>
<input type="checkbox" name="ProductID" value="3" />Product #3
<input type="textbox" name="Product3_Quantity">
<input type="textbox" name="Product3_Color">
</div>
Now, again, on the server you could parse the data in this way:
<%
Dim intID
Dim intQuantity
Dim strColor
For Each intID In Request.Form("ProductID")
' this is a selected item
intQuantity = Request.Form("Product" & intID & "_Quantity")
strColor = Request.Form("Product" & intID & "_Color")
Next
%>
You'd be able to perform validation and other logic on each group of selected items in this manner.
I have multiple forms on a page which pass an id to the controller via hidden inputs. As I am using strongly typed views for these I think I need to keep the Id for each of these to be the same. It works currently though I think it's bad practice. How should I handle this? In Django there are form prefix values is there an equivalent?
Avoid duplication of form input element ID in Django
Here are the two forms I am using:
<form action="/Course/CropImage" method="post">
<input id="CourseId" name="CourseId" type="hidden" value="<%= Model.CourseId %>" />
<input id="X" name="X" type="hidden" value="<%= Model.X %>" />
<input id="Y" name="Y" type="hidden" value="<%= Model.Y %>" />
<input id="W" name="W" type="hidden" value="<%= Model.W %>" />
<input id="H" name="H" type="hidden" value="<%= Model.H %>" />
<input type="submit" value="Crop" />
</form>
<form action="/Course/UploadImage" enctype="multipart/form-data" method="post">
<input id="CourseId" name="CourseId" type="hidden" value="<%= Model.CourseId %>" />
<label for="Image">Select Image:</label><input id="Image" type="file" name="Select Image"/>
<input type="submit" value="Upload" />
</form>
If you are having 2 view models (one for the crop, one for the upload) you can prefix them like this (you can use html helpers):
<form action="/Course/CropImage" method="post">
<input id="Crop_CourseId" name="Crop.CourseId" type="hidden" value="<%= Model.CourseId %>" />
<input id="Crop_X" name="Crop.X" type="hidden" value="<%= Model.X %>" />
<input id="Crop_Y" name="Crop.Y" type="hidden" value="<%= Model.Y %>" />
<input id="Crop_W" name="Crop.W" type="hidden" value="<%= Model.W %>" />
<input id="Crop_H" name="Crop.H" type="hidden" value="<%= Model.H %>" />
<input type="submit" value="Crop" />
</form>
<form action="/Course/UploadImage" enctype="multipart/form-data" method="post">
<input id="Upload_CourseId" name="Upload.CourseId" type="hidden" value="<%= Model.CourseId %>" />
<label for="Image">Select Image:</label><input id="Upload_Image" type="file" name="Upload.Image"/>
<input type="submit" value="Upload" />
</form>
and then bind attribute with the prefix to you controller actions like this:
public ActionResult CropImage([Bind(Prefix="Crop")]CropViewModel viewModel)
{
// do something
}
public ActionResult UploadImage([Bind(Prefix="Upload")]UploadViewModel viewModel)
{
// do something
}
This is not a bad practise. They are completely different forms so that makes the input element unique. You will not make your server code or client js/markup any more semantic by adding prefixes.
I always prefix my column-names with the table name. Here's the database-layout of my latest MVC-project (using strongly typed views and LINQ to SQL):
WeblogEntries:
- WeblogEntryId
- WeblogEntryHeaderText
- WeblogEntryBodyText
- WeblogEntryDate
WeblogComments:
- WeblogCommentId
- WeblogCommentBodyText
- WeblogCommentDate
WeblogErrors
- WeblogErrorId
- WeblogErrorExceptionMessage
- WeblogErrorExceptionStackTrace
- WeblogErrorDate
These naming conventions work great with the entity classes that gets generated using dbml-files.