[英]Input field text validation failing for summernote text area
我正在使用 Summernote 進行富文本編輯。 我的輸入字段具有必需的屬性。
<input
required
id="summernote"
formControlName="description"
[ngxSummernote]="config"
/>
我有提交按鈕。 如果文本區域為空、原始或無效,它將被禁用。 下面的代碼。
<button
mat-raised-button
class="btn btn-danger pull-right"
type="submit"
(click)="submitDoc()"
[disabled]="
questionFormGroup.pristine || questionFormGroup.invalid
"
>
Post Question
</button>
如果我在summernote編輯器中復制粘貼一些文本,按鈕將啟用但如果我執行ctrl + a並刪除文本編輯器中存在的所有文本,則應禁用該按鈕,因為文本區域內沒有文本。 如果我提交表單,文本將包含如下值:
<br><div>
</div><div>
</div><div>
</div><div>
</div><div>
</div><div>
</div><div>
</div><div>
</div><div>
</div><div>
我如何解決這個問題到summernote。 此處啟用按鈕的圖像和此處的文本區域內容。
這是我的表單組:
createQuestionFormGroup(): void {
this.questionFormGroup = this.formBuilder.group({
heading: undefined,
description: undefined,
});
}
將內容直接粘貼到ngxSummernote
庫文本框中后出現問題,沒有更新 formControl 的touched
/ pristine
formControl
的表單控件。
要解決此問題,您可以在config
的callbacks
選項中實現onPaste
回調 function 。
config = {
...,
callbacks: {
onPaste: (ne) => {
var bufferText = (ne.originalEvent || ne).clipboardData.getData('Text');
if (bufferText) {
// calling onTouched method to set formControl touched/pristine state update.
this.summernote.onTouched();
}
},
},
};
isEmpty
返回編輯器內容是否為空。 您可以實現此方法來檢查內容。
編輯區域需要<p><br></p>
獲得焦點,即使編輯器內容為空。 所以 Summernote 支持這種方法來幫助檢查編輯器內容是否為空。
if ($('#summernote').summernote('isEmpty')) {
alert('editor content is empty');
}
這是解決您的特定問題的另一種方法。 使用以下方法收集 Summernote 文本區域中存在的 HTML
var text = $('#body-summernote').summernote('code');
去掉 var 文本中存在的 HTML 標簽。
$(text).text()
您將擺脫所有標簽,例如和
$(text).text().trim().length > 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.