Skip to content

Commit 641ea36

Browse files
committed
Add validation
1 parent b1bc114 commit 641ea36

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/main/scala/app/GistController.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,15 @@ trait GistControllerBase extends ControllerBase {
249249
if(flatten){
250250
(0 to count - 1).flatMap { i =>
251251
(params.get(s"fileName-${i}"), params.get(s"content-${i}")) match {
252-
case (Some(fileName), Some(content)) if(fileName.nonEmpty && content.nonEmpty) => Some((fileName, content))
252+
case (Some(fileName), Some(content)) if(content.nonEmpty) => Some((if(fileName.isEmpty) s"gistfile${i + 1}.txt" else fileName, content))
253253
case _ => None
254254
}
255255
}
256256
} else {
257257
(0 to count - 1).map { i =>
258258
val fileName = request.getParameter(s"fileName-${i}")
259259
val content = request.getParameter(s"content-${i}")
260-
if(fileName.nonEmpty && content.nonEmpty){
261-
(fileName, content)
262-
} else {
263-
("", "")
264-
}
260+
(if(fileName.isEmpty) s"gistfile${i + 1}.txt" else fileName, content)
265261
}
266262
}
267263
}

src/main/twirl/gist/edit.scala.html

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@import view.helpers._
44
@html.main("Snippet"){
55
<div class="container">
6+
<div id="error"></div>
67
@if(gist.isEmpty){
78
<div>
89
<ul style="list-style: none; margin-left: 0px;">
@@ -101,15 +102,38 @@
101102

102103
$('.submit_snippet').click(function(){
103104
var count = $('#count').val();
104-
var editor = null;
105+
if(count == 0){
106+
displayError('File is required.');
107+
return false;
108+
}
109+
var value = null;
105110
for(var i = 0; i < count; i++){
106-
$('#content-' + i).val(ace.edit('editor-' + i).getValue());
111+
value = ace.edit('editor-' + i).getValue();
112+
if(value == ''){
113+
displayError('Files can\'t be empty.');
114+
return false;
115+
} else {
116+
$('#content-' + i).val(ace.edit('editor-' + i).getValue());
117+
}
107118
}
119+
return true;
108120
});
109121

110122
$('#delete').click(function(){
111123
return confirm('Are you positive you want to delete this Gist?');
112124
});
125+
126+
function displayError(message){
127+
$('#error' ).html(
128+
'<div class="alert alert-error">' +
129+
'<button type="button" class="close" data-dismiss="alert">&times;</button>' +
130+
message +
131+
'</div>'
132+
);
133+
$('html, body').animate({
134+
scrollTop: $("#error").offset().top
135+
}, 0);
136+
}
113137
});
114138
</script>
115139
}

0 commit comments

Comments
 (0)