Smatry replace not print array of variable - replace

{$variable.fieldname} = Username
My input <input type="text">
I have Tried {$variable|replace:"type=\"text\"":'type="text" placeholder="{$variable.fieldname}"'}
Getting Output: <input type="text" placeholder="{$variable.fieldname}">
Expected: <input type="text" placeholder="Username">

You need to assign an additional variable and use regex_replace method instead.
{assign var="newvar" value="type=\"text\" placeholder=\"{$variable.fieldname}\""}
{$variable|regex_replace:"/type=\"text\"/":{$newvar}}

Related

Parse arrays sent with form

See this answer. In short, it transforms:
<input type="text" name="object[0][color]" value="red">
<input type="text" name="object[0][doors]" value="2">
<input type="text" name="object[0][color]" value="green">
<input type="text" name="object[0][doors]" value="4>
Into a PHP associative array:
'object' => array(
'0'=>array(
'color'=>'red',
'doors'=>'2'
),
'1'=>array(
'color'=>'green',
'doors'=>'4'
)
)
In Django, I try to do the same, but when I print request.POST, I get:
<QueryDict: {'object[0][color]': ['red'], 'object[0][doors]': ['2'], 'object[1][color]': ['green'], 'object[1][doors]': ['4']}>
How to convert it to a proper dict?
This should work:
myDict = dict(queryDict.iterlists())

Angular2 pattern validator on Input

I'm trying to setup a pattern validator with the following regex :
^(((0|[1-9]\d{0,2})(\.\d{2})?)|())$
Try Regex here
That should allow me to get 1 to 3 digits, and then a decimal part of maximum 2 digits, and that should allow empty values as well.
The problem is that either my input is of type text and the validator is rejecting my input (any input since it's not considered as a digit I believe); or the input is of type number and without step="any" my input value is rejected if I have a decimal input (while the regex seems to be working on simpler values), and with step="any" it seems my regex is not working at all, allowing whatever value because of the step.
<form (ngSubmit)="onSubmit()" #bottleUpdatePriceForm="ngForm" >
<div class="form-group" *ngFor="let bottle of bottleArrayToUpdate; let i = index">
<label for="bottlePrice">{{bottle.name}} : </label>
<input type="number" class="form-control" name="bottlePrice" autocomplete="off" step="any"
[pattern]="pricePattern"
[(ngModel)]="bottleArrayToUpdate[i].price">
</div>
<button type="submit" class="btn btn-default">Submit</button>
<!--(click)="bottleUpdatePriceForm.reset();"-->
</form>
EDIT : adding my component code for regex binding
private pricePattern = /^(((0|[1-9]\d{0,2})(\.\d{2})?)|())$/;
Be it text or number I don't really care, I just need the pattern to work on my input... Any insight or something I am missing ?
Here is a working example in a plunkr : https://plnkr.co/edit/znVaS7?p=info
You can switch the input line in the plunkr to see the different cases :
<input type="text" class="form-control" name="bottlePrice" autocomplete="off"
<input type="number" class="form-control" name="bottlePrice" autocomplete="off" step="any"
Unrelated to the main issue : Is there any way to call the form reset from the component rather than directly in the template ?
==> bottleUpdatePriceForm.reset();
I was wondering, this is just for bonus.
Thanks a lot
This is not a direct solution for the not working regex, but this works with the same purpose. So remove the pattern and just change your input with max and min instead:
<input type="number" class="form-control" name="bottlePrice"
autocomplete="off" step="any" max="999" min="0"
[(ngModel)]="bottleArrayToUpdate[i].price">

Display empty default field in Django form

I would like to initialize my form with a default empty value.
In my views.py i have one like so:
form = BookingForm(initial={'name':''})
and in the template like so:
<tr><td><input class="input" type="text" name="name" id="id_name" placeholder="Enter your name" value={{ form.name }}/></td></tr>
The output however, in the html input field shows '/' instead of the placeholder.
Any ideas how to do this?
Try adding quotes around your {{ form.name }} like so:
<tr><td><input class="input" type="text" name="name" id="id_name" placeholder="Enter your name" value="{{ form.name }}"/></td></tr>
and if that doesn't change anything you can try in your forms.py:
name = forms.CharField(initial='')
rather than trying to set the default input value upon instantiation.

Spaces in string in <input type="text"> django templates

I have this code in a Django Template:
input type="text" name="ppoisNomePOI" value={{ request.session.ppoisNomePOI }}
If my "request.session.ppoisNomePOI" is "John" then in the page it appears "John" in the input, but if my "request.session.ppoisNomePOI" is "John Flanders Simpson" it only appears "John"
I assume it was a problem with the spaces in the value.
From the sounds of it, the browser is probably treating the subsequent words as empty attributes and being interpreted as:
<input type="text" name="ppoisNomePOI" value="John" Flanders Simpson>
Try putting quotes around the value.
value="{{ request.session.ppoisNomePOI }}"
This will force it to be interpreted as:
<input type="text" name="ppoisNomePOI" value="John Flanders Simpson">

Django nested formsets

I have an edit object view that contains a formset(one or many if this matters), now I want to create a page that can display multiple edit object forms and submit it in a single form.
What is the correct way to achieve this task?
I found a solution.
I can enumerate my objects on edit page and use different prefixes for formsets based on these indexes. Here is an example:
First, you need enumeration, I achieved it using same input(checkbox) name with incremental values:
<input type="checkbox" name="counter" value="0">
...
<input type="checkbox" name="counter" value="1">
...
Counter numbers is the formset and other data serial numbers:
<!--Ordinary inputs-->
<input type="text" name="data0" value="value0">
<input type="text" name="data1" value="value1">
<!--Formsets-->
<input type="text" id="test0-0-data" name="test0-0-data" value="something">
<input type="text" id="test0-1-data" name="test0-1-data" value="something">
<input type="hidden" name="test0-TOTAL_FORMS" id="id_test0-TOTAL_FORMS" value="2">
<input type="hidden" name="test0-INITIAL_FORMS" id="id_test0-INITIAL_FORMS" value="0">
<input type="text" id="test1-0-data" name="test1-0-data" value="something">
<input type="hidden" name="test1-TOTAL_FORMS" id="id_test1-TOTAL_FORMS" value="1">
<input type="hidden" name="test1-INITIAL_FORMS" id="id_test1-INITIAL_FORMS" value="0">
Then if code you populate formsets like this:
counter = request.POST.getlist('counter')
for i in counter:
TestFormset = modelformset_factory(Test, form=TestForm)
test_formset = TestFormset(request.POST, prefix='test'+i, queryset=Test.objects.none())
I achieved HTML structure above with JavaScript.