我正在使用此plugin来验证表单。我遇到的一个问题是其中一个文本字段是BBCode插件,因此在验证时该字段没有任何反应。
它所说的Write the body of your tutorial
下的textarea使用了一个bbcode插件,它添加了一堆HTML和一个单独的输入框,你必须使用这个代码来获取值
$('textarea.bbcode').sceditor('instance').val();
所以理想情况下我需要弄清楚如何将它连接到jQuery Form Validator
这是我的代码:
$("#creation-form").validate({
rules: {
title: {
required: true,
maxlength: 100,
minlength: 10
},
category: "required",
body: {
required: true,
},
tags: "required"
},
messages: {
title: 'Please enter the title of your tutorial'
},
errorElement: 'div',
errorPlacement: function(error, element)
{
error.addClass('form-error');
error.insertAfter(element);
}
})
HTML表单:
<form method="post" action="" enctype="multipart/form-data" id="creation-form">
@if(Content::hasContent('upload_tutorial'))
<div class="alert alert-info">
{{ Content::getContent('upload_tutorial') }}
</div>
@endif
@if(FormResponse::has())
<div class="box-wrapper">
<div class="box-content">
{{ FormResponse::get() }}
</div>
</div>
@endif
<div class="margin-bottom"></div>
<p>Tutorial Title</p>
<p><input type="text" name="title" class="form-control" maxlength="100" value="{{ $upload->title or "" }}"></p>
<p> </p>
<p>Tutorial Category</p>
<p><select name="category" class="form-control">
<option value="" selected>Select a category</option>
@foreach(TutorialCategory::all() as $tut)
@if(isset($upload->category) && $upload->category == $tut->name)
<option value="{{ $tut->name }}">{{ $tut->name }}</option>
@else
<option value="{{ $tut->name }}" checked>{{ $tut->name }}</option>
@endif
@endforeach
</select></p>
<p> </p>
<p>Write the body of your tutorial</p>
<p><textarea name="body" class="bbcode">{{ $upload->body or "" }}</textarea></p>
<p> </p>
<p>Add tags</p>
<input type="text" name="tags" id="tagField" class="form-control" value="{{ $upload->tags or "" }}">
<ul id="tags"></ul>
<p><b>Tags:</b>
@foreach(Tag::getTagsArray('tutorial') as $k => $tagName)
<span class="tag-name">{{ $tagName}}</span>
@endforeach
</p>
<p> </p>
<p>Select a picture</p>
<p><input type="file" name="picture" class="form-control"></p>
@if($upload != null && $upload->hasPicture())
<p> </p>
<img src="{{ $upload->getThumbnail() }}" style="max-height:150px">
@endif
<p> </p>
<button class="btn">Upload Tutorial</button>
</form>
答案 0 :(得分:1)
我创建了这个解决方案。
由于无法识别bbcode textarea
,因此我决定创建一个input
来检查textarea
。 Here is the source code
在 HTML 中,我添加了这一行:
<p><input class="form-control" id="texthidenid" name="texthiden" type="text" value=""></p>
这是JavaScrtip:
$(function() {
// Replace all textarea tags with SCEditor
$('textarea').sceditor({
plugins: 'bbcode'
});
$("#creation-form").validate({
rules: {
title: {
required: true,
maxlength: 100,
minlength: 10
},
category: "required",
texthiden: {
required: true,
depends: function(element) {
validateTextarea();
return $('textarea').sceditor('instance').val() !== '';
}
},
picture: "required"
},
messages: {
title: 'Please enter the title of your tutorial',
picture: 'Please enter the picture of your tutorial'
},
errorElement: 'div',
errorPlacement: function(error, element) {
error.addClass('form-error');
error.insertAfter(element);
}
})
function validateTextarea() {
var data = $('textarea').sceditor('instance').val();
if (data) {
var x = document.getElementById("texthidenid").value = data;
console.log(data);
}
}
});