Laravel custom field name in form request validation - laravel-5

I have a form fields in the view as:-
<input type="email" name="user[email]">
<input type="text" name="name">
<input type="text" name="designation">
While submitting this post request, I have a rule defined as:-
app\Http\Requests\StaffEditRequest.php
$rule['user.email'] = 'email|unique:users';
However, when laravel tries it to validate the request it queries the database as if there is a field name "user.email" in the users table. How to customize the field name so that I can tell laravel that I am looking for email field in the users table and not user.email?

You need to define the column in in unique parameter. Do like following:
$rule['user.email'] = 'email|unique:users,email';
Ref: Laravel Doc

Related

Missing request attribute 'projektId' of type String | Thymleaf Form with just a String

I'm working on a Projekt where you can add workers to projects with their ids.I am using springboot, thymeleaf and a database means you give a project and a worker Id and the programm adds the worker to the project.workerlist. The Problem ist that I get this error:
Required request parameter 'projektId' for method parameter type String is not present
My HTML Form looks like this
<form action="#" th:action="#{neuenMitarbeiterzuProjektHinzufuegen}" method="post">
Projekt ID: <input type="text" th:value="*{projektId}" required/><br>
Mitarbeiter ID: <input type="text" th:value="*{mitarbeiterId}" required/><br>
<br>
<input type="submit" value="Mitarbeiter hinzufügen"/>
<input type="reset" value="Clear"/>
</form>
My Post Route Handler Method looks like this
#PostMapping(value="/neuenMitarbeiterzuProjektHinzufuegen")
public String neuenMitarbeiterzuProjektHinzufuegen(#RequestAttribute(value = "projektId") String projektID, #RequestAttribute(value = "mitarbeiterId") String mitarbeiterID,Model m)
{
Optional<Projekt> projekt = projektRepository.findById(Long.parseLong(projektID));
projektRepository.findById(Long.parseLong(projektID)).get().mitarbeiterHinzufuegen(mitarbeiterRepository.findById(Long.parseLong(mitarbeiterID)).get());
return "redirect:Projekte";
}
Looking at your code example I think you should be using #RequestParam not #RequestAttribute. Param is for things posted from the user (web) side and attribute you can set on the server side.
This blog has some explanation on the difference of #RequestAttribute https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/request-attribute.html

fill form fields from relationship model in vform vue laravel

I have used vue vform for my form, here is the function to edit modal,
editModal(household){
this.form.reset();
$('#addNewHouseholdModal').modal('show');
this.form.fill(household); // this line fills the form with data to be edited
}
But I have form fields for which data is stored in another table. I have received the data but mnot able to figure out how to display them in the form. Please help me with it. Below is the household data i have passed in my form above.
id: 1
address_details_id: 14
ward: 2
house_no: "2"
family_no: "45"
geolocation: "{"latitude":"1.1","longitude":"1.1"}"
address_details: Object
id: 14
province: "23"
name: "Strret"
All details are filled in the form except Address details which is an object, how do I fill name from address details in the form? Thank you. Help will be appreciated.
I think you need to pass first the data before you open the modal.
editModal(data) {
isEditting = true
this.form.family_no = data.family_no
this.form.house_no = data.house_no
this.form.address = data.address
$('#addNewHouseholdModal').modal('show');
}
and then you form must be like this
<modal>
<input type="text" v-model="form.family_no" />
<input type="text" v-model="form.house_no" />
<input type="text" v-model="form.address" />
<modal>

List element access in spring form using index

Controller Coding
List<Map<String,Object>> rows=globalDao.list("select * from hosp_conf");
System.out.println(rows.get(0).get("hosp_id"));
model.addObject("list",rows);
return model;
View File
<input type="text" name="name" value="${rows.get(0).get("hosp_name")}" required>
I want to access my list element in spring form using specific index.
Above Sysout stament print excat value in console
How can I access it?
In jsp you can access a list index using the syntax -
${list[index]}.
In your jsp[, you need to use model attribute name that you have added at your controller.So instead of use rows you need to use list.
I don't know about the object in your list. But you can try like this -
<input type="text" name="name" value="${list[0]["hosp_name"]}" required>

Play 2.0 framework - POST parameters

I'm trying to POST parameters to Action, and wrote in the routes:
# Home page
GET / controllers.Application.index()
POST /login/name:/password: controllers.Application.login(name, password)
and I have an Action
public static Result login(String name, String password) {
return ok(name + " " + password);
}
my form is
<form action="/login" method="post">
<input name="name" type="text" id="name">
<input name="password" type="password" id="password">
<input type="submit" value="Login">
</form>
And it doesn't work
For request 'POST /login' [Missing parameter: name]
What am i doing wrong?
Simply change the route to the following:
POST /login controllers.Application.login(name, password)
By NOT including the dynamic names (:name and :password) in the routing path, the assumption is that the variables come from the request (IE: your html inputs)
The error you are getting indicates that name and password do not appear in the url path... which is correct because the path you specified in your routes indicates the path should look something like this:
/login/myname/mypassword
Please check http://www.playframework.org/documentation/2.0.1/JavaRouting and look at the section called "Call to action generator method"
your route should not include dynamic parts (name, password) since the data is in the body and not the url
Though an old post, but if anyone new comes to the question. We should not add parameters, when you are using post, also if you did use parameters, it would be
GET /login/:name/:password controllers.Application.login(name: String, password: String)
For post, don't add parameters and bind it to a case class inside the controllers and access the variables.

Validate a Hidden Field

I'm using MVC3 with unobtrusive validation. I have a field that the user is expected to fill with some data and then press a "search" button. If search has never been pressed or the user has changed the input field after pressing search, the form should not be possible to submit.
I've added a hidden field that is set to true by the click() event of the button and emptied by the keyup() event of the input box. Now I would like to add a validation rule that requires the hidden field to be true to allow submit.
Preferably I would like to use unobtrusive validation, but if that doesn't work it is ok with something that requires some javascript, as long as it doesn't spoil the unobtrusive validation for the rest of the form.
The following code snippet does exactly what I want, until I add type="hidden".
<input class="required" id="client-searched" data-val="true"
name="ClientSearched" data-val-required="Press search!"/>
<span class="field-validation-valid" data-valmsg-replace="true"
data-valmsg-for="ClientSearched"/>
try
var validator = $("#myFormId").data('validator');
validator.settings.ignore = "";
Here is an informative blog post
EDIT
#RAM suggested a better solution please FOLLOW
I had a similar problem, and I used this code to change defaults, in MVC 4:
#Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$.validator.setDefaults({
ignore: ""
})
</script>
Source:
JQuery validate
In some cases you want just ignore validation on one or several
hidden fields (not all hidden field) in client side and also you want validate them and other hidden fields in server side.
In these cases you have validation attributes for all hidden fields in your ViewModel and they will be used to validate the form when you post it (server side).
Now you need a trick to just validate some of the hidden fields in client side (not all of them). In these cases i recommend you to use my mechanism!
Set data-force-val as true in the target hidden input tags. It's our custom attribute that we use to detect target hidden inputs witch we want validate them in client side.
// This hidden input will validate both server side & client side
<input type="hidden" value="" name="Id" id="Id"
data-val-required="The Id field is required."
data-val="true"
data-force-val="true">
// This hidden input will validate both server side & client side
<input type="hidden" value="" name="Email" id="Email"
data-val-required="The Email field is required."
data-val="true"
data-force-val="true">
// This hidden input just will validate server side
<input type="hidden" value="" name="Name" id="Name"
data-val-required="The Neme field is required."
data-val="true">
Also you can set data_force-val for your hidden inputs by jQuery:
$("#Id").attr("data-force-val", true); // We want validate Id in client side
$("#Email").attr("data-force-val", true); // We want validate Email in client side
$("#Name").attr("data-force-val", false); // We wont validate Name in client side (This line is not necessary, event we can remove it)
Now, active data-force-val="true" functionality by some simple codes like these:
var validator = $("#TheFormId").data('validator');
validator.settings.ignore = ":hidden:not([data-force-val='true'])";
Note: validator.settings.ignore default value is :hidden

Resources