uploading a PDF in grails - grails

I need to upload a PDF in grails I have looked in texts and online. This is what I have so far in my controller
def upload = {
def f = request.getFile('myFile')
if(!f.empty) {
f.transferTo( new File('/some/local/dir/myfile.txt') )
response.sendError(200,'Done');
} else {
flash.message = 'file cannot be empty'
render(view:'uploadForm')
}
}
In the view I have this in the _forms view
<g: uploadForm action="upload">
<input type="file" name="myFile"/>
<input type= "submit" value="Upload"/>
</g: uploadForm>
When I go to the create page to I get an error.
I get this error
Grails tag [g:] was not closed. Stacktrace follows:
Message: Error processing GroovyPageView: Grails tag [g:] was not closed
Line | Method
->> 461 | doFilter in \grails-app\views\report\create.gsp
Caused by GrailsTagException: Grails tag [g:] was not closed
->> 36 | doCall in C:/Users/Owner/Desktop/grails-app/views/report/create.gsp
Any suggestion on how to clear the error? When I delete the addition to the _forms view the error goes. I can't see the unclosed tag
Full gsp as asked for
<div class="fieldcontain ${hasErrors(bean: reportInstance, field: 'location', 'error')} required">
<label for="location">
<g:message code="report.location.label" default="Location" />
<span class="required-indicator">*</span>
</label>
<g:select id="location" name="location.id" from="${lc.Location.list()}" optionKey="id" required="" value="${reportInstance?.location?.id}" class="many-to-one"/>
</div>
<div class="fieldcontain ${hasErrors(bean: reportInstance, field: 'published', 'error')} required">
<label for="published">
<g:message code="report.published.label" default="Published" />
<span class="required-indicator">*</span>
</label>
<g:datePicker name="published" precision="day" value="${reportInstance?.published}" />
</div>
<div class="fieldcontain ${hasErrors(bean: reportInstance, field: 'title', 'error')} ">
<label for="title">
<g:message code="report.title.label" default="Title" />
</label>
<g:textField name="title" value="${reportInstance?.title}"/>
</div>
<div class="fieldcontain ${hasErrors(bean: reportInstance, field: 'myFile', 'error')} ">
<label for="myFile">
<g:uploadForm action="upload"/>
<input type="file" name="myFile"/>
<input type= "submit" value="Upload"/>
</g:uploadForm>
</label>
</div>

Remove / from
<g:uploadForm action="upload"/>
This should be
<g:uploadForm action="upload">
....
</g:uploadForm>

Related

How to compare emails in javascript?

I am trying to make an edit profile page in which user can update its current email or pwd by inputting current email and password and then give new ones. I am also sending whole data of user(including username and password) when he sign in through viewdata.
#{
var user = (User)ViewData["User"];
}
<div class="account-info mt-3">
<fieldset class="border border-dark p-3">
<legend class="float-none w-auto">Account Info</legend>
<div class="mb-3 simpleinfo">
<label for="email" class="form-label">Enter Your Current Email</label>
<input type="email" class="form-control" name="Username" id="prevemail"/>
</div>
<div class="mb-3 simpleinfo">
<label for="prevpwd" class="form-label">Enter Your Current Password</label>
<input type="password" class="form-control" name="Pwd" id="prevpwd"/>
<button type="button" class="btn btn-success mt-4 checkcred">Update Credentials</button>
</div>
<div class="mb-3 simpleinfo">
<label for="email" class="form-label">Enter new Email</label>
<input asp-for="Username" disabled type="email" class="form-control" id="newemail">
</div>
<div class="mb-3 simpleinfo">
<label for="newpwd" class="form-label">Enter New Password</label>
<input type="password" disabled class="form-control" asp-for="Password" id="newpwd">
</div>
</fieldset>
</div>
Now i want to compare the email that user enters and the email that is present in the user object.
here's my code for this:
<script>
$('.checkcred').click(function() {
var prevemail = $('#prevemail').val();
var prevpassword=$('#prevpwd').val();
if(prevemail==#user.Username&&prevpassword==#user.Password)
{
alert("Validated");
}
else
{
alert("not validated");
}
});
</script>
But it is giving me syntax error on Console: Invalid or unexpected token.
Please lemme know what i am doing wrong.

Filling form in chromium (CEF)

How can I fill a password field on CEF?
I try this:
CodeStr := 'document.forms[0].form_password.value="hahaha";';
Chromium1.Browser.MainFrame.ExecuteJavaScript(CodeStr, 'mainFrame.GetURL()', 0);
but its not work...
The source code is:
<div class="field">
<input type="password" name="password" id="form_password" value="" size="20" regex="^.{6,}$" class="required" aria-required="true">
</div>

Best way to prevent HTML Code in Textbox MVC Razor View

I have following code on my view.
This HTML code is used for search in website.
This view is not strongly typed view, so I can apply use DataAnnotation through model.
What is best view to validate that, this textbox should accept only alpha numeric characters?
HTML
<form action="Search" method="post" >
<div class="col-md-8">
<input type="text" name="name" placeholder="search" class="fullwidth" onkeypress="return BlockingHtml(this,event);" />
</div>
<div class="col-md-4">
<input type="submit" title="Search" value="Search" />
</div>
</form>
Javascript
function BlockingHtml(txt) {
txt.value = txt.value.replace(/[^a-zA-Z 0-9\n\r.]+/g, '');
}
Model:-
[StringLength(100)]
[Display(Description = "Name")]
[RegularExpression("(/[^a-zA-Z 0-9\n\r.]+/g", ErrorMessage = "Enter only alphabets and numbers of Name")]
public string Name{ get; set; }
Updated:-
View:-
<form action="Search" method="post" >
<div class="col-md-8">
<input type="text" name="name" id="txt" placeholder="search" class="fullwidth" onkeypress="BlockingHtml(this);" />
</div>
<div class="col-md-4">
<input type="submit" title="Search" value="Search" />
</div>
</form>
function BlockingHtml(txt) {
txt.value = txt.value.replace(/[^a-zA-Z 0-9\n\r.]+/g, '');
}
<form action="Search" method="post" >
<div class="col-md-8">
<input type="text" name="name" placeholder="search" class="fullwidth" onblur="return BlockingHtml(this,event);" />
</div>
<div class="col-md-4">
<input type="submit" title="Search" value="Search" />
</div>
</form>
Changed event onkeypress to onblur.

Field error in object 'talent.CandidateProfile' on field 'core': rejected value [null];

1:declared field name as a name="db_address1"
<div class="fieldcontain ${hasErrors(bean: candidateProfileInstance, field: 'core.db_address1', 'error')} ">
<label for="core.db_address1">
<g:message code="candidateProfile.core.db_address1.label" default="Dbaddress1" />
</label>
<g:textField **name="db_address1"** value=""/>
</div>
declared field name as a name="core.db_address1"

grails g:field error not showing

I have this setup, but the validation is not working
index.gsp:
<g:form name="loginForm" autocomplete="off" controller="company" action ="save">
<table >
<tr>
<td><g:field type="text" name="company" required="true" value="${c?.companyName}" /></td>
</tr>
</table>
Controller:
def index = {
def c =new Company()
//c=params
return [c:c]
}
def save ={}
You need to check for errors using hasErrors method in your form:
<div class="fieldcontain ${hasErrors(bean: yourBean, field: 'yourField', 'error')} required">
<label for="yourField">
<g:message code="yourBean.yourField.label" default="yourField" />
<span class="required-indicator">*</span>
</label>
<g:textField name="content" required="" value="${yourBean?.yourField}"/>
</div>
Grails hasErrors documenttaion.
And check in your controller (save action) if the validation has passed using:
if (!yourBean.save(flush: true)) {
render(view: "create", model: [yourBean: yourBean])
return
}

Resources