将只读输入值发送到服务器(MEAN堆栈)

huangapple go评论86阅读模式
英文:

Send readonly input value to server(MEAN stack)

问题

我有一个表单,其中有一个只读字段和其他普通字段,如姓名、身高、体重、出生日期。我无法将只读字段的值发送到服务器(MongoDB)。没有只读字段,我尝试发送表单并且一切正常。

当我尝试使用只读字段时,我收到错误消息:

"failed{"errors":{"id":{"message":"Path id is req…Path id is required.","name":"ValidationError"}}}

以下是您提供的代码的翻译部分:

export class HomeComponent {

  getname = localStorage.getItem('key');
  id = Date.now()

  constructor(private service: RecordService) { }

  onsubmit(form: NgForm) {
    this.service.postRecords(form.value).subscribe(res => {
      console.log(res);
    });
  }
}
<div class="container">
    <h2 class="text-center">欢迎 {{ getname }}</h2>
    <form #myForm="ngForm" (ngSubmit)="onsubmit(myForm); myForm.reset()">
        
        <div class="form-group"> 
            <label>会员ID</label>
            <input type="number" name="id" [value]="id" class="form-control" readonly>
        </div>

        <div class="form-group"> 
            <label for="name">姓名</label>
            <input ngModel required name="name" pattern="[A-Za-z]+" #name="ngModel" minlength="4" type="text" class="form-control" id="name">
            <div class="alert alert-danger" *ngIf="name.touched && !name.valid">
                <div *ngIf="name.errors.required">姓名是必填项。</div>
                <div *ngIf="name.errors.minlength">姓名至少需要4个字符。</div>
                <div *ngIf="name.errors.pattern">姓名应该只包含字母,不包含空格。</div>
            </div>
        </div>

        <!-- 其他表单字段的翻译 -->

        <button type="submit" class="btn btn-success" [disabled]="!myForm.form.valid">提交</button> 
        <button type="submit" class="btn btn-success" (click)="myForm.reset()">重置</button>
    </form>
</div>
export class Record {
    id: Number;
    name: String;
    dob: String;
    height: Number;
    weight: Number;
}
const mongoose = require("mongoose");

const schema = mongoose.Schema({
  id: {
    type: Number,
    required: true
  },
  name: {
    type: String,
    required: true
  },
  dob: {
    type: String,
    required: true
  },
  height: {
    type: Number,
    required: true
  },
  weight: {
    type: Number,
    required: true
  }
});

const Records = (module.exports = mongoose.model("Records", schema));

希望这可以帮助您解决问题。如果您需要进一步的帮助,请随时提出。

英文:

I have a form which has one readonly and other normal fields like name, height, weight, DOB. I am unable to send the value of read-only field to the server(MongoDB). Without the read-only field, I tried sending the form and its working fine.                    

I get error

> "failed{"errors":{"id":{"message":"Path id is req…Path id is
> required.","name":"ValidationError"}"}

when i try with readonly field.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

export class HomeComponent {
getname=localStorage.getItem(&#39;key&#39;);
id = Date.now()
constructor(private service:RecordService) { }
onsubmit(form: NgForm) {
this.service.postRecords(form.value).subscribe(res =&gt; {
console.log(res);
});
}
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
&lt;h2 class=&quot;text-center&quot;&gt;Welcome {{getname}}&lt;/h2&gt;
&lt;form  #myForm=&quot;ngForm&quot; (ngSubmit)=&quot;onsubmit(myForm); myForm.reset()&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt; 
&lt;label&gt;Member ID&lt;/label&gt;
&lt;input  type=&quot;number&quot; name=&quot;id&quot; [value]=&quot;id&quot; class=&quot;form-control&quot; readonly&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt; 
&lt;label for=&quot;name&quot;&gt;Name&lt;/label&gt;
&lt;input ngModel required name=&quot;name&quot; pattern=&quot;[A-za-z]+&quot; #name=&quot;ngModel&quot; minlength=&quot;4&quot; type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;name&quot;&gt;
&lt;div class=&quot;alert alert-danger&quot; *ngIf=&quot;name.touched &amp;&amp; !name.valid&quot;&gt;
&lt;div *ngIf=&quot;name.errors.required&quot;&gt;Name is required.&lt;/div&gt;
&lt;div *ngIf=&quot;name.errors.minlength&quot;&gt;Name requires atleast 4 characters.&lt;/div&gt;
&lt;div *ngIf=&quot;name.errors.pattern&quot;&gt;Name should contain only alphabets without space.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt; 
&lt;label for=&quot;dob&quot;&gt;Date of Birth&lt;/label&gt;
&lt;input  type=&quot;date&quot; class=&quot;form-control&quot; id=&quot;dob&quot; name=&quot;dob&quot; #dob=&quot;ngModel&quot; ngModel required&gt;
&lt;div class=&quot;alert alert-danger&quot; *ngIf=&quot;dob.touched &amp;&amp; !dob.valid&quot;&gt;
&lt;div *ngIf=&quot;dob.errors.required&quot;&gt;Date of Birth is required.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt; 
&lt;label for=&quot;height&quot;&gt;Height&lt;/label&gt;
&lt;input  type=&quot;number&quot; placeholder=&quot;Enter in cms&quot; class=&quot;form-control&quot;  id=&quot;height&quot; name=&quot;height&quot; #height=&quot;ngModel&quot; ngModel required&gt;
&lt;div class=&quot;alert alert-danger&quot; *ngIf=&quot;height.touched &amp;&amp; !height.valid&quot;&gt;
&lt;div *ngIf=&quot;height.errors.required&quot;&gt;Height is required.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt; 
&lt;label&gt;Weight&lt;/label&gt;
&lt;input  type=&quot;number&quot; placeholder=&quot;Enter in Kilograms&quot; class=&quot;form-control&quot; id=&quot;weight&quot; name=&quot;weight&quot; #weight=&quot;ngModel&quot; ngModel required&gt;
&lt;div class=&quot;alert alert-danger&quot; *ngIf=&quot;weight.touched &amp;&amp; !weight.valid&quot;&gt;
&lt;div *ngIf=&quot;weight.errors.required&quot;&gt;Weight is required.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- &lt;a routerLink=&quot;/table&quot;&gt;    --&gt;
&lt;button type=&quot;submit&quot;class=&quot;btn btn-success&quot; [disabled]=&quot;!myForm.form.valid&quot;&gt;Submit&lt;/button&gt; 
&lt;!-- &lt;/a&gt; --&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot; (click)=&quot; myForm.reset()&quot;&gt;Reset&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

export class Record{
id:Number;
name:String;
dob:String;
height:Number;
weight:Number;
}

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const mongoose = require(&quot;mongoose&quot;);
const schema = mongoose.Schema({
id: {
type: Number,
required: true
},
name: {
type: String,
required: true
},
dob: {
type: String,
required: true
},
height: {
type: Number,
required: true
},
weight: {
type: Number,
required: true
}
});
const Records = (module.exports = mongoose.model(&quot;Records&quot;, schema));

<!-- end snippet -->

答案1

得分: 0

你可以使用 "disabled" 而不是 "readonly"。

英文:

you can use the "disabled" instead-of "readonly"

答案2

得分: 0

我弄清楚了,我只是改变了

<input  type="" name="id"  [value]="id" class="form-control" readonly>

<input  type="" name="id"  [(ngModel)]="id" class="form-control" readonly>
英文:

I figured it out I just changed

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;input  type=&quot;&quot; name=&quot;id&quot;  [value]=&quot;id&quot; class=&quot;form-control&quot; readonly&gt;

<!-- end snippet -->

to

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;input  type=&quot;&quot; name=&quot;id&quot;  [(ngModel)]=&quot;id&quot; class=&quot;form-control&quot; readonly&gt;

<!-- end snippet -->

答案3

得分: 0

有两种方法来完成这项任务:

  1. 您可以为只读字段创建一个隐藏字段,其值将与只读字段绑定,因此一旦提交表单,隐藏输入将被传递。[请记住为隐藏字段提供您要传递的名称属性,而不是只读字段,因为隐藏字段将被传递,而不是只读字段]。

  2. 第二种方法是在提交表单时从所有输入中移除禁用属性。这样,您可以发送所有输入的值。

英文:

There are two ways to do the Job:

  1. You can create a hidden field for the readonly field whose value will be binded with the readonly field so once you submit the form the hidden input will be passed.[ Remember to give the name attribute you want to pass to the hidden field rather than the readonly field as the hidden field will be passed and not eh readonly field].

  2. Second way is to remove the disable property from all the input at the time your are subumitting your form. In this way you can send the value of all the inputs.

huangapple
  • 本文由 发表于 2020年1月3日 19:56:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定