I can't modify an associative table SPRING BOOT - spring

Good evening to all,
I have an error when I want to modify an entity.
I have my post entity, another error entity, and an associative postError entity.
So I want to modify the errors chosen for the concerned item on the updatePosteErreur form.
I already managed the case for the addition of the itemError, and it works.
However, when I want to modify the itemError by modifying the errors, I have this error message:java.lang.IllegalArgumentException: Parameter value [6] did not match expected type [com.MailleCoTech.SuiviProduction.entities.Poste (n/a)]
I don't understand...
Here is my service and my updateView:
<th:block th:each="erreur: ${erreurs}">
<input class="form-label mt-4" type="checkbox" name="erreurs" th:field="*{erreurs}" th:value="${erreur.id}"/>
<label th:text="${erreur.name}"></label>
<br>
</th:block>
if(!posteUpdateForm.getErreurs().isEmpty()){
List<PosteErreur> posteErreurs = posteUpdateForm.getErreurs().stream().map(erreur -> {
PosteErreur posteErreur = posteErreurService.findByIdPoste(posteUpdateForm.getId());
posteErreur.setId(posteErreur.getId());
posteErreur.setIdErreur(erreur);
posteErreur.setIdPoste(poste);
return posteErreur;
}).collect(Collectors.toList());
this.posteErreurService.save(posteErreurs);
}
return poste;
I don't put all the code, because I have the same view to add, and an add function in the service and everything works, it's when I modify that I get this error message :/
thank you in advance for your help and your time

Related

model is not defined in ajax call response in mvc

I am sending model and checkbox value data to the controller from the view. While at run time it showing me error i.e. model is not defined. This error is showing me when I call the ajax in mvc view.
Here is my code,
#model VendorModel
<input type="checkbox" id="chb_wholesale" onclick="WholeSaleCheck()" />#T("WholeSale")
<script>
function WholeSaleCheck() {
alert(Model.vendorId);
}
</script>
Now, I am surprised that why showing me this type of error because Model also available and after tying the Model word showing list of their property field also one of the field is vendorId and that also automatically**(by pressing ctrl+space)**.
Got the solution, by coding this #Model.vendorId error is solve out.

Angular 2 form valid by default

Having issue with form validation .
i want to submit the form only when form is valid.
but with the empty inputs and clicking on submit button is submitting the form although the inputs are empty.
<form name="equipmentForm" #f="ngForm" (ngSubmit)="f.form.valid && addEquipment()" validate>
Inputs be like this.
<input name="equimentId" class="text-input form-control" type="text" [(ngModel)]="model.equipmentNumber" pattern="^[0-9][0-9]{1,19}$" title="Equipment ID. can be upto 20 digits only.">
I cant post the whole code although.
this
f.form.valid is true from form initialization
wanted to acheive something like this
<div *ngIf="!model.equipmentModel && f.submitted" class="text-danger">
Please enter Equipment Model
</div>
So on submit i want to show this message instead of default browser's.
but this f.form.valid is goddamn true from default.
You should add required attribute to your input tags to, then as #Cobus Kruger mentioned, form will not be submitted untill it is filled.
However you can also give a try to pristine, dirty options, which allow you to check if the user did any changes to the form so in this case your condition may look like this:
<form name="equipmentForm" #f="ngForm" (ngSubmit)="f.form.valid && f.form.dirty ? addEquipment() : ''" validate>
and the input:
<input name="equimentId" class="text-input form-control" type="text" [(ngModel)]="model.equipmentNumber" pattern="^[0-9][0-9]{1,19}$" title="Equipment ID. can be upto 20 digits only." required />
In this case it will check if any changes were applied to the input, and submit the form if both conditions are met.
If you specify the required attribute on the input, then the form will not be submitted unless a value is filled in. But that only covers values that were not supplied and you may want to check for invalid values as well.
The usual way is to disable the submit button unless the form is valid. Like this:
<button type="submit" [disabled]="!f.form.valid">Submit</button>
The Angular documentation about form validation also shows this. Look near the bottom of the "Simple template driven forms" section
In function which you call on submit you can pass form as parameter and then check. In html you will need to pass form instance:
<form name="equipmentForm" #f="ngForm" (ngSubmit)="addEquipment(f)" validate>
In typescript:
addEquipment(form){
if(form.invalid){
return;
}
//If it is valid it will continue to here...
}

How to style successful input fields in Thymeleaf

I would like to use Bootstrap's has-success and has-failure classes with Thymeleaf.
So far I have
<div th:class="${#fields.hasErrors('field')}? 'form-group has-error' : 'form-group'"></div>
This displays the failure style correctly, when the form is posted and the field is invalid.
However if I change the second part of the ternary to 'form-group has-success', then on the initial form GET request, then, of course, it styles it as a success, even though the form hasn't been posted yet.
My question: is there a way in Thymeleaf to handle the following
Displays a form without any styling on GET.
On POST apply has-error or has-success classes.
I think you'll need to add attributes to your Model in the back-end for this.
In you GET request, change nothing. In your POST request, add an attribute: ["hasErrors", true] if the form data you send via the post is incorrect, false otherwise.
Now in your html you can add the following:
<th:block th:if="${hasErrors != null}">
<div th:class="${hasErrors ? 'form-group has-error' : 'form-group has success'"></div>
</th:block>
<th:block th:unless="${hasErrors != null}">
<div class="form-group"></div>
</th:block>
You check if the hasErrors model attribute isn't null, if it is, it means you're in the GET method and you should display a simple form-group. If the hasErrors is not null, you can create the ternary expression based on the boolean value hasErrors. The th:block is non-html. You can replace it with a div, but then you neen an extra div just to check a boolean.
I'm not going into GET/POST problem but I think that this can help you:
New th:errorclass for adding CSS class to form fields in error
Until now, whenever we wanted to apply a specific CSS class to an input field in a form when there were errors for that field, we needed to use the th:class or th:classappend attributes.
In Thymeleaf 2.1, in order to simplify this structure, a new th:errorclass attribute processor has been introduced. This processor will read the name of the field from the name or th:field attribute in the same tag, and apply the specified class if such field has errors.
Note the 'error' literal is in fact a token, so no single quotes are really needed.
The result is much more concise. Note also that th:errorclass works like th:classappend, not th:class. So the specified class will in fact be appended to any existing ones.
http://www.thymeleaf.org/whatsnew21.html#errcl
I found that Thymeleaf as a hasAnyErrors function.
<div class="form-group row"
th:attrappend="class=${#fields.hasAnyErrors()
? #fields.hasErrors('field') ? ' has-error' : ' has-success'
: '' }">
This now works.
When the user GETs the form, hasAnyErrors is false, so the empty string is appended and the input and label receive the default style.
When the user POSTs the form, if there are any errors then the first part of the ternary is evaluated. This adds the has-error or has-success styles.
This was inspired by Roel Strolenberg's answer below.

Laravel 4 - Showing edit form with OLD data input as well as DB information

Im making a edit form for my app and i was wondering if someone could tell me how to get the data from the database into my text field.
I can locate the record i need to edit based on the users click, and i can display the information if i do the following:
value="{{ $letter->subject }}"
BUT, the problem im having is that when i run it through the validation and there is an error, it comes back with the database information instead of the OLD data.
So my questions is. Is there a way to serve up the database information first and then when it goes through the validatior, validate the information the user has edited?
Currently to validate the text field and bring the data back incase of error, im using
Input::old('subject')
Is there a parameter for that old bit that allows me to put in the DB data?
Cheers,
Hey you could validate and return ->withInput() and then in your actual form, check if there is Input::old() and display it, otherwise display from the db.
example:
<input type="text" name="subject"
value="{{ (Input::old('subject')) ? Input::old('subject') : $letter->subject }}">
Or you could go the other way and define the variable and do a regular if statement, instead of the ternary one! Up to you to decide what you want to use!
All you need is form model binding http://laravel.com/docs/html#form-model-binding:
{{ Form::model($letter, ['route' => ['letters.update', $letter->id], 'method' => 'put']) }}
// your fields like:
{{ Form::text('someName', null, ['class' => 'someHTMLclass' ...]) }}
// no default values like Input::old or $letter->something!
{{ Form::close() }}
This way you form will be populated by the $letter data (passed from the controller for example).
Now, if you have on your countroller:
// in case of invalid data
return Redirect::back()->withInput();
then on the redirect your form will be repopulated with input values first, not the original model data.
Make it more simple and clean
<input type="text" name="subject" value="{{ (Input::old('subject')) ?: $letter->subject }}">
I'm not sure for Laravel 4 but in Laravel 5, function old takes second param, default value if no old data in session.
Please check this answer Best practice to show old value

Show all Spring form errors on Velocity template

I've got an application using Spring MVC and Velocity. On one of my forms, I want to show all errors related to the form at the top of the page. I've figured out how to show errors related to one particular field (using the #springShowErrors macro), but I really want to have one large block of errors at the top of the form, instead of listing the errors next to each individual item.
I've done quite a bit of googling, and a few people have suggested something like
#if ($status && $status.errors.hasErrors())
#foreach( $error in $status.errorMessages )
<p>$error</p>
#end
#end
...but this gives me no output when placed just below the initial #springBind macro that attaches my command object to the form. Putting #springShowErrors just after the #springFormInput macro for each field works fine, so I know my validator is running and generating errors.
Any ideas? Have I missed something really silly?
Here's the complete form, with my non-working attempt just after the first #springBind
<form name="standardForm" id="standardForm" method="post" action="#springUrl("/requestAccess")">
#springBind("accessRequest")
#if ($status && $status.errors.hasErrors())
#foreach( $error in $status.errorMessages )
<p>$error</p>
#end
#end
<fieldset>
<label for="name">Name</label>
#springFormInput("accessRequest.name" " ")
<label for="company">Company</label>
#springFormInput("accessRequest.company" " ")
<label for="title">Title</label>
#springFormInput("accessRequest.title" " ")
<label for="email">Email</label>
#springFormInput("accessRequest.email" " ")
<button type="submit" value="send">Send</button>
</fieldset>
</form>
Thanks for any help or advice!
I think you're on the right track. There's no direct way I know of to get all the object error messages and field error messages in one aggregated list, however you can do this:
#springBind("bindName")
#if($status.errors.hasErrors())
## Global error messages
#foreach($e in $status.errorMessages)
<p>${e}</p>
#end
## Field error messages
#foreach($f in $status.errors.fieldErrors)
#springBind("bindName.${f.field}")
#foreach($e in $status.errorMessages)
<p>${e}</p>
#end
#end
#end
Not so clean, but it works.

Resources