placeholder attribute is invalid when check it in browser - html

I assign values to placeholder within every tag like:
<div class="form-group">
<label for="firstname" class="col-sm-1 control-label">FirstName</label>
<div class="col-sm-11">
<input type="text" class="form-control" id="firstname" value= {{ form.first_name.value }} name="first_name" placeholder="first_name">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-1 control-label">Lastname</label>
<div class="col-sm-11">
<input type="text" class="form-control" id="lastname" name="last_name" value= {{ form.first_name.value }} placeholder="last_name">
</div>
</div>
However, when I check it on browser, it displays None.
How to solve such a problem?

Pass the value in string format as shown below:
value="{% if form.first_name.value %}{{ form.first_name.value }}{%else%}{%endif%}"
This will fix the issue because if value attribute's value is empty then only placeholder attribute's value comes into picture!

What you're seeing is actually exactly what you wrote. form.first_name.value is None if not set on the form, so it sets value="None" and lo, that is what the form dutifully displays in preference to the placeholder.
You need to get a '' for your default value if a field.value is None. One way would be an explicit check using {% if form.first_name.value %} {{ form.first_name.value }} {% endif %}. More concisely, {{ form.first_name.value|default_if_none:"" }} using a template filter.

Related

How do you assign a value to an input field in HTML?

How do you assign a value to an input field in HTML, this is a python and jinja, the {{ fullname }} contain the complete name two or more words and the value is there, but my code only display the first word.
this is the code:
<div class="form-group">
<input type="text" name="Fullname" class="form-control" placeholder="Company Name" value={{ fullname }} required />
</div>
Let say that the value returned by Database is: juan Perez
it will only display juan, not the complete name.

Normal text field workable but once i add in form control it stop working

Currently I have this form where the inputs are normal textfield (Basic look) and it works. But once i add in form control, it stop working.
In my HTML (One part of the text field): Workable
<div class="col-md-5">
<div class="form-group">
<label for="{{form.hostname.id_for_label}}">Hostname</label>
{{form.hostname}}
</div>
</div>
But if i change it to the following codes: Not workable
<div class="col-md-5">
<div class="form-group">
<label for="{{form.hostname.id_for_label}}">Hostname</label>
<input class="form-control" type="text" id="{{form.hostname.id_for_label}}" name="{{form.hostname}}"
placeholder="" required>
</div>
</div>
Am i doing something wrong? Appreciate if anyone could help
You are not using braces and you are not specifying the name attribute of the field for the name. Something like this:
<input class="form-control" type="text"
id="{{ form.hostname.id_for_label }}"
name="{{ form.hostname.name }}"
{% if value %} value="{{ form.hostname.value }}"{% endif %}
placeholder=""
{% if form.hostname.field.required %}required{% endif %}>
Instead of manually rendering like this you may want to look into django crispy forms or django floppyforms.
If you need to add custom class names, then you can overide your form like this
def __init__(self, *args, **kwargs):
super(form_name, self).__init__(*args, **kwargs)
self.fields['hostname'].widget.attrs.update({'class': 'form-control'})
Add these to your forms.py under the form which your using, this will add the custom class to the respective field

Regex match multiple lines before and after word delimited by start and end words

I am wanting to search for {{ upc }} and start the capture not from the <div immediately ahead of the match but the 2nd <div ahead of the match i.e. <div class="form-group"> and capture not up to the first </div> after the match but the 2nd i.e closing </div> or up to the start of the next <div class="form-group"> (depending on how you look at it)
Here is the sample HTML/Twig template I am wanting to search and replace.
<div class="form-group">
<label class="col-sm-2 control-label" for="input-sku"><span data-toggle="tooltip" title="{{ help_sku }}">{{ entry_sku }}</span></label>
<div class="col-sm-10">
<input type="text" name="sku" value="{{ sku }}" placeholder="{{ entry_sku }}" id="input-sku" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-upc"><span data-toggle="tooltip" title="{{ help_upc }}">{{ entry_upc }}</span></label>
<div class="col-sm-10">
<input type="text" name="upc" value="{{ upc }}" placeholder="{{ entry_upc }}" id="input-upc" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-ean"><span data-toggle="tooltip" title="{{ help_ean }}">{{ entry_ean }}</span></label>
<div class="col-sm-10">
<input type="text" name="ean" value="{{ ean }}" placeholder="{{ entry_ean }}" id="input-ean" class="form-control"/>
</div>
</div>
The expected regex match is as follows:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-upc"><span data-toggle="tooltip" title="{{ help_upc }}">{{ entry_upc }}</span></label>
<div class="col-sm-10">
<input type="text" name="upc" value="{{ upc }}" placeholder="{{ entry_upc }}" id="input-upc" class="form-control"/>
</div>
</div>
All help appreciated. Thank you.
One thing you can try is to use a negative lookahead to filter out the things you do not wish to be included in your match. For instance, matching a <div, followed by anything and then another <div, can match things like <div></div><div>.
Instead, what you can say is to match <div, followed by anything - as long as it is not </div> - and then another <div.
<div (?:(?!</div>).)* <div
Then, you can insert that same subpattern anywhere in your expression where you'd normally write .*. In this particular case, you can repeat that to make sure you're not hitting a closing div before the UPC and then continue with the {{ UPC }} portion.
<div(?:(?!</div>).)*<div (?:(?!</div>).)* {{ upc }} .*?</div>\s*</div>
Here is a demo
You need to parse the div's you want and then absorb everything inside them and exclude the rest.
[\w\W] means match words and non-words. It matches newline characters for instance, which * does not.
[\w\W]*(<div[\w\W]*?<div[\w\W]*?{{ sku }}[\w\W]*?<\/div>[\w\W]*?<\/div>)[\w\W]*

Drop down list in laravel form

I have been working to build a laravel form and for the fields where text input is required I have successfully created. The following part works.
<fieldset>
<legend><span class="number">1</span> Personal Information</legend>
<input type="text" name="name" placeholder="Name *" value="{{old('name') }}">
<div>{{ $errors->first('name') }} </div>
<input type="text" name="email" placeholder="Email *" value="{{old('email') }}">
<div>{{ $errors->first('email') }} </div>
<input type="text" name="phone" placeholder="Phone *" value="{{old('phone') }}">
<div>{{ $errors->first('phone') }} </div>
However, I want to make a drop down list of countries (and after this two or three other drop down lists). This is the HTML part of it. How do I use a drop down list in Laravel where the selected country will appear in the email I receive after the user submits the form?
<label for="nationality">Nationality:</label>
<select id="nationality" name="nationality">
<option value="select_nationality">*</option>
<option value="india">Afghanistan</option>
<option value="india">Albania</option>
<option value="india">Algeria</option> (includes all the countries in the list)

how to change form field container div class?

I using symfony2.5. I change form field container div class.
generated form fields:
<div>
<label for="ddd" class="required">lname</label>
<input type="text" id="acme_demobundle_default_lname" name="acme_demobundle_default[lname]" required="required">
</div>
<div>
<label for="dddd" class="required">fname</label>
<input type="text" id="acme_demobundle_default_fname" name="acme_demobundle_default[fname]" required="required">
</div>
I add class to there are input`s container div.
any idea thanks
You can set attributes using the form builder inside your controller
$builder->add('lname', 'text', array('attr' => array('class'=>'something')))
If you have this in your form:
$builder->add('name')
In your template you should use the functions:
{{ form_label(form.name) }} {# access the label #}
{{ form_widget(form.name) }} {# access the input field #}
You can wrap that up with your div and style it however you want:
<div class="your-class">
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
</div>
You can check more on How to Customize Form Rendering