Lift - Snippet Failure Method not found - scala

I've added this to my Boot.scala
LiftRules.viewDispatch.append({
case List("admin", "categories") => Right(Admin)
})
Here is my Admin.scala in (admin/view package)
object Admin extends LiftView{
def dispatch = {
case "add" => editCategory
}
def editCategory(): NodeSeq = {
<lift:embed what="/admin/add_category"></lift:embed>
}
}
Here is my add_category template -
<lift:surround with="admin" at="content">
<div class="container-fluid" id="main_container">
<lift:Admin:addCategory form="POST" class="form-horizontal">
<fieldset>
<legend>Add Category</legend>
<div class="control-group">
<label class="control-label" for="cat_name">Name</label>
<div class="controls">
<cat:name></cat:name>
</div>
</div>
<div class="control-group">
<label class="control-label" for="cat_desc">Description</label>
<div class="controls">
<cat:desc></cat:desc>
<cat:submit></cat:submit>
</div>
</div>
</fieldset>
</lift:Admin:addCategory>
</div>
</lift:surround>
I'm trying to bind this with a snippet -Admin.scala in (admin/snippet package) with addCategory method.
object name extends RequestVar("")
object desc extends RequestVar("")
def addCategory(in: NodeSeq): NodeSeq = {
def doAdd() {
//todo: Save Category
}
bind("cat", in,
"name" -> SHtml.text(name.is, (n: String) => name(n), ("id", "cat_name"), ("class", "input-large")),
"desc" -> SHtml.textarea(desc.is, (d: String) => desc(d), ("id", "cat_desc"), ("class", "input-large")),
"submit" -> SHtml.submit("Save", doAdd, ("class", "btn btn-info"))
)
}
I'm getting this error -
Error processing snippet: admin:addcategory
Reason: Method Not Found
XML causing this error:
<lift:admin:addcategory class="form-horizontal" form="POST">
<fieldset>
<legend>Add Category</legend>
<div class="control-group">
<label for="cat_name" class="control-label">Name</label>
<div class="controls">
<cat:name></cat:name>
</div>
</div>
<div class="control-group">
<label for="cat_desc" class="control-label">Description</label>
<div class="controls">
<cat:desc></cat:desc>
<cat:submit></cat:submit>
</div>
</div>
</fieldset>
</lift:admin:addcategory>
And in the logs -
[net.liftweb.http.LiftRules] - Snippet Failure: SnippetFailure(/admin/categories/add -> ParsePath(List(admin, categories, add),,true,false),Full(admin:addcategory),Method Not Found)
Need help with this. I'm unable to figure out why lift is not able to find the method.

Have you added your admin package into LiftRules? Something like this should allow Lift to search admin.snippets for resolution:
LiftRules.addToPackages("admin")
Also, I believe you need to be calling the snippet as Admin.addCategory instead of with a :.
Edit:
I believe that the Lift HTML5 parser was case sensitive and had trouble with camel case method names. You may want to try renaming your method to all lowercase, or try calling your snippet as (instead of with the <lift: style):
<div class="lift:admin.addCategory"> ... </div>
or
<div data-lift="admin.addCategory"> ... </div>

Related

how to define an entity inside the function symfony 5

for exemple this is my function :
/**
* #Route("/companyProfile/{id_Adresse}/{id_Employes}/{id_Contact}", name="company_profile")
* #ParamConverter("Adresse", options={"mapping": {"id_Adresse" : "id"}})
* #ParamConverter("Employes", options={"mapping": {"id_Employes" : "id"}})
* #ParamConverter("Contact", options={"mapping": {"id_Contact" : "id"}})
*/
public function index(Adresse $adresse , Employes $employes , Contact $contact,ContactRepository $contactrepository, EmployesRepository $employesrepository , AdresseRepository $adresserepository ): Response
{
$contactform=$this->createForm(ContactType::class, $Contact);
$employesform=$this->createForm(EmployesType::class, $Employes);
$adresseform=$this->createForm(AdresseType::class, $Adresse);
return $this->render('companyProfile.html.twig', [
'Contact' => $contactrepository->findAll(),
'Employes' => $employesrepository->findAll(),
'Adresse' => $adresserepository->findAll(),
'contactform'=>$contactform->createView(),
'employesform'=>$employesform->createView(),
'adresseform'=>$adresseform->createView()
]);
}
as you can see i declare all my entity in the parameter but i need to add all there ids in the route and that's not what i need , i just want my route to be like this :
/*
*#Route ("/companyProfile", name="company_profile")
*/
thanks in advance for all your answer
The param converter goal is the retrieve an entity from a route parameter. My guess is that you want to get all this entities but not with their ids in the route.
To do so, you will need to get the repository for each of your entity and then get them here. There is a question still : how do you know which entity do you want?
Here is an exemple of your action
/**
* #Route("/companyProfile", name="company_profile")
*/
public function index(ContactRepository $contactRepository, EmployesRepository $employesRepository , AdresseRepository $adresseRepository): Response
{
$contactform = $this->createForm(ContactType::class, $contactRepository->find($contactId);
$employesform = $this->createForm(EmployesType::class, $employesRepository->find($employesId);
$adresseform = $this->createForm(AdresseType::class, $adresseRepository->find($adresseId);
return $this->render('companyProfile.html.twig', [
'Contact' => $contactrepository->findAll(),
'Employes' => $employesrepository->findAll(),
'Adresse' => $adresserepository->findAll(),
'contactform'=>$contactform->createView(),
'employesform'=>$employesform->createView(),
'adresseform'=>$adresseform->createView()
]);
}
You still need a way to get all this new ids variables, but I can't help you more with the given informations.
i tried a different way to display them but when i want to edit for exemple contact , it dosen't work and they show me this error:
An exception has been thrown during the rendering of a template ("Notice: Undefined index: Contact").
here's a screenshot:
enter image description here
and here's my code :
/**
* #Route("/companyProfile/", name="company_profile")
*/
public function index(): Response
{
$Contact = $this->getDoctrine()
->getRepository(Contact::class)
->findAll();
$Employes = $this->getDoctrine()
->getRepository(Employes::class)
->findAll();
$Adresse = $this->getDoctrine()
->getRepository(Adresse::class)
->findAll();
return $this->render('companyProfile.html.twig', [
'Contact' => $Contact,
'Employes' => $Employes,
'Adresse' => $Adresse,
]);
}
this is the edit function:
/**
* #Route("/UpdateContact/{id}",name="editcontact")
*/
public function EditContact(Contact $contact , Request $request ,ManagerRegistry $manager)
{
$contactform=$this->createForm(ContactType::class, $contact);
$contactform->handleRequest($request);
if($contactform->isSubmitted() && $contactform->isValid())
{
$manager = $this->getDoctrine()->getManager();
$manager->persist($contact);
$manager->flush();
return $this->redirectToRoute('company_profile');
}
return $this->render('companyProfile.html.twig', [
'contactform'=>$contactform->createView()
]);
}
and last my twig :
<div class="boxed-list margin-bottom-60">
<div class="boxed-list-headline">
<h3><i class="icon-material-outline-business-center"></i> les contactes d'entreprise</h3>
</div>
</br>
{% for Contact in Contact %}
<div class="listings-container compact-list-layout">
<ul class="list-group">
<li class="list-group-item">
<h3>Contact</h3>
</br></br>
<div class="container pt-3">
<h5>Responsable:</h5>
<p>{{Contact.responsable}}</p>
<h5>N° de tele:</h5>
<p>{{Contact.telephone}}</p>
<h5>Addresse email:</h5>
<p>{{Contact.email}}</p>
<h5>Note:</h5>
<p>{{Contact.note}}</p>
<div class="centered-button margin-top-35">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Modifier
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<!-- Form -->
<form method="post" id="add-note">
<div class="form-group">
<label for="nom">Responsable:</label>
<input type="text" class="form-control" placeholder="{{Contact.responsable}}" id="nom">
</div>
<div class="form-group">
<label for="tele">N° de tele:</label>
<input type="text" class="form-control" placeholder="{{Contact.telephone}}" id="telephone">
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" placeholder="{{Contact.email}}" id="email">
</div>
<label for="note">Note:</label>
<textarea name="textarea" cols="10" placeholder="{{Contact.note}}" class="with-border"></textarea>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button><i class="icon-material-outline-arrow-right-alt"></i>
</div>
</div>
</div>
</div>

Why validation is not working?

corporate.html
<form #f="ngForm" name="corporateProfileForm" ng-submit="corporateFrmSave(objDS, objDSCurrency)" novalidate="">
<div class="row form-group">
<div class="col-md-12" >
<input type="text" *ngIf="formData" [(ngModel)]="formData.Corporate_Id" id="corpid" title="Corporate ID" tab-index="1" name="corpid" maxlength="20" #corpid="ngModel" required/>
<label for="corp"><b>Corporate ID</b></label>
<div *ngIf="corpid.invalid && (corpid.dirty || corpid.touched)" class="alert alert-danger">
<div *ngIf="corpid.errors.required">
Name is required.
</div>
<div *ngIf="ncorpidame.errors.minlength">
Name must be at least 4 characters long.
</div>
<div *ngIf="corpid.errors.forbiddenName">
Name cannot be Bob.
</div>
</div>
</div>
</div>
</form>
Iam getting error message as
ERROR TypeError: Cannot read property 'invalid' of undefined
Make sure you define your variable with this structure:
TS file
export class ContactComponent implements OnInit {
myform: any;
corpid: FormControl;
constructor() {
}
ngOnInit() {
this.createFormControls();
this.createForm();
}
createFormControls() {
this.corpid= new FormControl('', Validators.required);
}
createForm() {
this.myform = new FormGroup({
corpid: this.corpid,
});
}
HTML file
<div class="md-form form-sm form-group"
[ngClass]="{
'invalid': corpid.invalid && (corpid.dirty || corpid.touched),
'valid': corpid.valid && (corpid.dirty || corpid.touched)
}">
<i class="fa fa-user prefix"></i>
<input
type="text" id="corpid" class="form-control"
required
[formControlName]="'corpid'">
<label for="corpid" class="">Your corpid</label>
</div>
<div class="form-control-feedback"
*ngIf="corpid.errors && (corpid.dirty || corpid.touched)">
<p *ngIf="corpid.errors.required">corpid is required</p>
</div>
It's hard to tell because issue is not in your HTML snipped but in .ts file.
I hope my Code will be helpfull
The issue is with the *ngIf, so when that condition is checked, Angular renders the rest of the template, and while the small fraction of time while *ngIf is being evaluated, your template reference variable corpid does not exist, so it will throw an error when trying to check the validation intially.
Wrap the input field with the validation div's inside the *ngIf instead of applying it only on the input field.
<div class="row form-group" *ngIf="formData">
<input [(ngModel)]="formData.Corporate_Id" id="corpid" ... >
<!-- more code here... -->
</div>
DEMO

Play framework Scala Controller method called twice

I'm facing an issue while calling a Play Scala controller method with a large amount of data. I have file upload method which takes 2 uploaded files and processes them. If the file size is big, the controller method is getting called twice. If the file size is less, it works fine and is getting called only once. I'm not sure what is happening behind the scene.
routes.conf
POST /test/upload controllers.TestController.upload(id:Int, isTest: Boolean)
TestController.scala
def upload(id: Int, isTest: Boolean) = Action(parse.multipartFormData) { implicit request: Request[play.api.mvc.MultipartFormData[play.api.libs.Files.TemporaryFile]] =>
.....
.....
}
upload.scala.html
#helper.form(action = routes.TestController.upload(form.id, isTest), 'enctype -> "multipart/form-data") {
<div class="form-group">
<label for="file1">File 1:</label>
<input class='btn btn-outline btn-primary' name="file1" type="file" data-preview-file-type="text" >
</div>
<div class="form-group">
<label for="file1">File 2:</label>
<input class='btn btn-outline btn-primary' name="file2" type="file" data-preview-file-type="text" >
</div>
<button type="submit" class="btn btn-default btn-primary" id="uploadButton" onclick="$('#myPleaseWait').modal('show');">
Upload</button>
}
Any help on why the method is getting called twice will be helpful.

How can I add parameters to a Play template without commas nor order?

I'd like to know how to improve my own kind of template's helper. For instance, I made an autocomplete.scala.html that receives parameters in order to be dynamic, something like:
#autocomplete("elementLabel","data-url-where-its-going-to-get-JSON-data","anyother","etc","foo")
Then I can use autocomplete component in any page I want to.
But I'm wondering how to make it easier to use, for instance, Play's inputText helper has the option you can add parameters using " ' " + parametername and then -> (probably lambda?) so, how could i implement it, so, I could, for instance make something like:
#autocomplete("data-url",'someattribute->"value", '_anotherOneWithUnderscoreWhyBtw->"whyunderscore")
autocomplete.scala.html
#**
* Componente para pesquisa textual e sele��o de registro de relacionamento 1 para N
*
* Example:
*
* #autocomplete("example.nested.id", "Unidade", "/sistema/buscarExemplo", myForm.get().getUnidade.getId.toString, myForm.get().getUnidade.getLabel.toString)
*#
#(fieldId: String, fieldTitle: String, dataUrl: String, initIdValue: String, initTitleValue: String)
<div class="form-group">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<label for="#fieldId" class="control-label">#fieldTitle</label>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<span class="input-icon input-icon-right" style="width:100%">
<input type="text" class="autocomplete-relation"
name="#fieldId"
data-url="#dataUrl"
data-id-status="#fieldTitle"
data-init-id-value="#initIdValue"
data-init-nome-value="#initTitleValue"
style="width:100%"
/>
<i id="response_#fieldTitle" class="icon-search blue"></i>
</span>
</div>
</div>
As soon as new requirements appear i must add one more parameter and then modify it at all pages i call it. I wanted to use that component like that original checkbox, from play, for instance:
#checkbox(myForm("myField"),'_label ->"MY_LABEL", 'class->"MYCLASS")
OR
#checkbox(myForm("myField"),'class ->"MYCLASS",'_label->"MY_LABEL")
or even
#checkbox(myForm("myField"),'class ->"MYCLASS",'_label->"MY_LABEL",'anotheattribute->"VALUE")
and so on. How could I do it so I wouldn't have to fill all of them and the order wouldn't matter?
You could use repeated (String, String) tuples as the last parameter:
#(fieldId: String, fieldTitle: String, data: (String, String) *)
<div class="form-group">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<label for="#fieldId" class="control-label">#fieldTitle</label>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<span class="input-icon input-icon-right" style="width:100%">
<input type="text" class="autocomplete-relation"
name="#fieldId"
data-id-status="#fieldTitle"
#data.map{ case (key, value) =>
#{key}="#{value}"
}
style="width:100%"
/>
<i id="response_#fieldTitle" class="icon-search blue"></i>
</span>
</div>
</div>
And you'd call it the way you're wanting:
#autocomplete("someId", "someTitle", "data-url" -> "google.com", "data-attr" -> "someValue")
You could also use a Map[String, String].
#(fieldId: String, fieldTitle: String, data: Map[String, String])
The rest of the template would remain the same, but you'd pass it something like this:
Map[String, String](
"data-url" -> "google.com",
"data-field-something" -> "value"
)

multiple verify method on form tuple

I'm quite new to play and scala. I'm working on form and validations. But I couldn't figure out to get all errors from multiple verification on form.
My form tuple looks like;
val companyMapping = Forms.tuple(
"name" -> nonEmptyText,
"email" -> email,
"password" -> nonEmptyText(8),
"re-password" ->nonEmptyText(8)).verifying(
// Add an additional constraint: both passwords must match
"Passwords don't match", data => {
data._3 == data._4 }
).verifying(
// Second constraint
"Test error", data => {
false }
)
In the view I print global errors and errors, it looks like;
#println(companyForm.globalError)
#println(companyForm.errors)
and output;
Some(FormError(,Passwords don't match,WrappedArray()))
List(FormError(,Passwords don't match,WrappedArray()), FormError(,Test error,WrappedArray()))
At this stage I have absolutely no idea about how to print both of the errors. I'm showing errors separately for the each input and show global errors at the end.
But if passwords match I can see test constraint in the global errors. Other than it only shows password match constraint.
Here is the view part;
#helper.form(action = routes.Login.register) {
<div class="row">
<span class="label">Name</span>
<input type="text" name="name" placeholder="Company Name" value="#companyForm("name").value" >
#if(!companyForm.errors("name").isEmpty){
<span class="error">#Messages(companyForm.errors("name")(0).message,"Company name")</span>
}
</div>
<div class="row">
<span class="label">Email</span>
<input type="text" name="email" placeholder="Email" value="#companyForm("email").value" >
#if(!companyForm.errors("email").isEmpty){
<span class="error">#Messages(companyForm.errors("email")(0).message,companyForm.errors("email")(0).key)</span>
}
</div>
<div class="row">
<span class="label">Password</span>
<input type="password" name="password" placeholder="Password" value="#companyForm("password").value" >
#if(!companyForm.errors("password").isEmpty){
<span class="error">#Messages(companyForm.errors("password")(0).message,8)</span>
}
</div>
<div class="row">
<span class="label">Re-type Password</span>
<input type="password" name="re-password" placeholder="Re-type your password" value="#companyForm("re-password").value" >
#if(!companyForm.errors("re-password").isEmpty){
<span class="error">#Messages(companyForm.errors("re-password")(0).message,8)</span>
}
</div>
#println(companyForm.globalError)
#println(companyForm.errors)
<div class="row">
<span class="label"><button type="submit">Save</button></span>
#companyForm.globalError.map { error =>
<span class="error">#error.message</span>
}
</div>
}
Maybe I'm just confused about those error types. So please can you explain it detailed.
In the re-password section of your template, you currently test if !companyForm.errors("re-password").isEmpty but then only show the message for companyForm.errors("re-password")(0), i.e. the first error only. Even if you have multiple errors.
You have to iterate over companyForm.errors("re-password") to print something for each error.
You can for example output a <span class="error">... for each error, using a for comprehension:
<div class="row">
<span class="label">Re-type Password</span>
<input type="password" name="re-password" placeholder="Re-type your password" value="#companyForm("re-password").value" >
#for (error <- companyForm.errors("re-password")) {
<span class="error">#Messages(error.message,8)</span>
}
</div>
See the play doc for Scala templates for other useful syntax to use in templates.