How to add name value to input element - angular5

I am looping a data, from the data, I am adding the name value into the input field, but not at all set. what is the issue here?
My form template:
<form novalidate name="myForm">
<div ng-show="myForm[addVas.name].$error.pattern">Error will come </div>
<div class="form-group">
<input type="text" [(ngModel)]="addVas.input" [attr.disabled]="addVas.disabled ? '' : null " name="{{addVas.name}}" ng-pattern="/^[0-9]/" (blur)="addAdnlVasAfterInput(addVas)" placeholder="Provide value of shipment"
class="form-control">{{addVas.name}} <!--getting value here-->
</div>
</form>
I am not getting throw the error, when user input instead of number in to charters. how to solve that?
Now I have update my name field in to [name]="addVas.name" but I not confirm the name sent, unless if i get error message

There are some confusions between Angular versions. ng-show should be *ngIf or [hidden] with reverse logic, ng-pattern is [pattern]. [attr-disabled] can be [disabled], etc.. Pattern /^[0-9]/ doesn't allow more than 1 digit, I am not sure it was your aim. If you use a property as pattern expression, the use [pattern]="property":
Here is what I suggest:
<form #myForm>
<input type="text" [(ngModel)]="addVas.input"
[disabled]="addVas.disabled" [pattern]="addVas.pattern"
[name]="addVas.name" #input="ngModel">
<div *ngIf="input.errors && (input.dirty || input.touched)" >
<div [hidden]="!input.errors.pattern">
Should be a number
</div>
</div>
</form>
Demo

The properties cannot perform interpolation.
You need to use property binding for setting values to properties.
Try this:
[name]="addVas.name"
instead of
name= "{{addVas.name}}"

Related

How to reset the values of an input and select in VUE?

I have an input and a select in my VUE application that are shown depending on the selection of a previous input.
If I insert values and change the initial input selection, I hide the second input and select, to restart the form.
The problem comes when I restart the form and I still get the selected values on the first load.
I'm trying to reset the values but none of the methods I've found in similar case reviews works fine.
Here's my select and my input that i want to reset values
<h1 v-if="this.isSelected"> What's your selection ?</h1>
<select
ref="item"
class="form-control"
id="selectedItem"
v-model="itemFormInfo.selectedItem"
#change="onChangeItem($event)">
<option v-for="item in filteredItem" v-bind:key="item.name">{{ item.name }}</option>
</select>
<div v-show="this.isItemSelected">
<h1>what's your item name ?</h1>
<input
type="text"
id="name"
ref="itemName"
class="form-control fields"
style="text-transform: capitalize"
v-model="itemFormInfo.name"
#keypress="formChange($event)"
/>
<div class="loader-spinner" v-if="loading">
<app-loader/>
</div>
</div>
and here's my method where I have tried the reset and document.getElementById('').value("") methods;
onChangeSpecie(event) {
let specie = event.target._value;
this.specieName = this.getSpecieName(specie);
this.breedName = this.getBreedName(specie);
this.$refs.breed.focus();
if (this.isBreedSelected = true) {
this.isBreedSelected = false;
this.isNameCompleted = false;
this.isLoaderFinished = false;
this.$refs.animalName.item()
}
},
If I print by console I see how the value is emptied but the input appears with the written value until I focus on the .
How do I stop it from appearing?
in neither method does it erase my previous values showing on the input.
what am I doing wrong?
thank you all for your help and time in advance
I believe you need to null the model not the target element's value.
this.itemFormInfo.name = null

only first word from radio button selection is being returned

To get the selected option from a radio button form, I'm looking at request.vars to get the value. Following is the controller code:
def get_results():
test = request.vars['QuestionOne']
for topic in session.selectedtopics:
test = test + request.vars[topic]
return locals()
And now the code for the view:
{{extend 'layout.html'}}
<P>Please select the options that most closely approximates the actual scenario.
<br>
<form action="{{=URL('get_results')}}" method="post">
{{nameTopic =""}}
{{session.selectedtopics = list()}}
{{for topic in topics:}}
{{if nameTopic <> topic.topic.replace(" ", "_"):}}
<br><h2>{{=topic.topic}}</h2>
{{nameTopic = topic.topic.replace(" ", "_")}}
{{session.selectedtopics.append(nameTopic)}}
{{pass}}
<p><input type="radio" name={{=nameTopic}} value={{=topic.param}}>{{=topic.param}}</p>
{{pass}}
<br>
<input type="submit">
</form>
Here is the problem: I don't know the reason, but it is getting only the first word of the selected option in the radio form. For example, the option selected is "It is normal", but the var is returning only "It". Any idea why it is happening?
Thank in advance.
You need to quote the HTML attribute values:
<input type="radio" name="{{=nameTopic}}" value="{{=topic.param}}">
Without the quotes, you'll end up with final HTML like:
<input type="radio" name=some_topic value=It is normal>
The browser will interpret the value to be "It", with "is" and "normal" being invalid HTML attributes.

Vue - conditional attribute binding

I am making a form in a Vue component and would like to set the HTML attribute required to an input field based on the value I am having in an object property.
So, for the example, an object that has fields like this:
label:"Name"
required:"1"
type:"textbox"
I need to a set the field to have required attribute in an input tag:
<input class="input is-large" :type="input.type" required>
And for the ones that don't have 1 as a value for that field I don't want a required attribute.
How can I do that in Vue?
You can do it like this:
<input class="input is-large" :type="input.type" :required="obj.required == 1">
Since your object's required property has 1 as a string not number I used == for comparison so that equality is tested after coercion

Input hidden is built without value

Weird this one.
On my .NET MVC 4 project I've added a file on App_Code who contains this method:
#helper CheckBox(string name, bool isChecked = false, string className = "") {
<div class="checkboxHolder">
<input id="#name" name="#name" type="hidden" value="#isChecked") />
<i class="#className checkboxBts fa #((isChecked) ? "fa-check-square-o" : "fa-square-o")" data-checkbox-associated="#name"></i>
</div>
}
I'm using it to style checkboxes using font-awesome, so my app checkboxes are made of an input type hidden who stores a boolean value and an icon to give feedback to users.
Weird thing is, on executing when isChecked == false, the hidden returned by this method is like:
<input id="myCheckboxId" name="myCheckboxId" type="hidden" />
There is no value at all, when I try to save it to the model an exception is thrown saying that model cannot be saved.
I've fixed it changing the method to use:
<input id="#name" name="#name" type="hidden" #((isChecked) ? "value=true" : "value=false") />
Which is working fine. However, I wonder if anyone know what could be happening on the original output.
Thank you all.
It's not entirely a duplicate, but this is answered in Why is my hidden input writing: value=“value” instead of true/false?:
if you have:
<input name="somefield" type="hidden" someprop="#(SomeBooleanExpression)"/>
[and #SomeBooleanExpression] is false it is omitted completely:
<input name="somefield" type="hidden"/>
To get around this, consider .ToString()
So, use:
<input id="#name" name="#name" type="hidden" value="value="#(isChecked.ToString())" />

How to get value of hidden input field not in same the order with regular expression

I want to get value of hidden input field
<FORM onsubmit="javascript:return add();" method=post action=addMess.php>
....
<INPUT name=sysess2 value=1234 type=hidden>
....
</FORM>
eg :
<INPUT type=hidden name=hyds2 value=1234>
<input name=hyds2 type=hidden value=1234>
<INPUT value=1234 type=hidden name=hyds2>
Anyone know how I can do that with regular expression ?
To get upper or lowercase, I use this regex (?i)(?<=\<input.*value\=).*(?=\>)
Perhaps with the use of pipe (|) for this regex I can get multiple matches ?