Laravel validator on a form and send post - laravel

I used laravel validator.
I have a form wih 2 fields
If i fill one field "NAME" and i leave the other "SURNAME" empty pressing SAVE button of the form appears required alert message on SURNAME but the name inserted in the NAME field became empty! And i think form send data anyway.why?I aspect that form doesn't send datas

In the value attribute of each input you need to echo old("the-input-name") so it can retain the previous input before the error
<input name="name" value="{{old('name')}}" class="" />

You should use old('NAME') to get the value of the field after the validation fails, blade example:
<input type="text" value="{{ old('NAME') }}" />
An example In case of update forms you should display the original value from the database unless there's an error:
<input type="text" value="{{ old('NAME') ?? $your_entry->value }}" />
Take a look at the documentation for more details

Related

How to keep the fields of a form after being redirected by login or registration in Laravel?

The business logic is as follows:
Fill out the form to publish a card.
Click on publish card button.
Publish if only if the user is authenticated.
If you are not authenticated, request to log in with the option to register.
If the user is registered or logged in, redirect to the form keeping the fields filled.
This is the logic of what I want to do
Note:
I currently show the login view if it is not authenticated with the help of laravel's Authentication Middleware.
The login view has the option to register.
I do not have much experience in laravel, I would like you could be explicit in your answer.
Try this one
<input type="text" class="form-control" placeholder="Name" name="name" required value="{{ old('name') }}">
this keep old value when you return page.
if you want redirect page with data, use Compact.
for example
$data["name"] = "name"
$data["gender"] = "something"
return view('your blade file path', compact('data'));
and on blade file
<input type="text" class="form-control" placeholder="Name" name="name" required value="{{ $data['name'] }}">

Thymeleaf set default value [duplicate]

I am programming in Spring and using Thymeleaf as my view, and am trying to create a form where users can update their profile. I have a profile page which lists the user's information (first name, last name, address, etc), and there is a link which says "edit profile". When that link is clicked it takes them to a form where they can edit their profile. The form consists of text fields that they can input, just like your standard registration form.
Everything works fine, but my question is, when that link is clicked, how do I add the user's information to the input fields so that it is already present, and that they only modify what they want to change instead of having to re-enter all the fields.
This should behave just like a standard "edit profile" page.
Here is a segment of my edit_profile.html page:
First Name:
Here is the view controller method that returns edit_profile.html page:
#RequestMapping(value = "/edit", method = RequestMethod.GET)
public String getEditProfilePage(Model model) {
model.addAttribute("currentUser", currentUser);
System.out.println("current user firstname: " + currentUser.getFirstname());
model.addAttribute("user", new User());
return "edit_profile";
}
currentUser.getFirstname() prints out the expected value, but I'm getting blank input values in the form.
Thanks.
Solved the problem by removing th:field altogether and instead using th:value to store the default value, and html name and id for the model's field. So name and id is acting like th:field.
I'm slightly confused, you're adding currentUser and a new'd user object to the model map.
But, if currentUser is the target object, you'd just do:
<input type="text" name="firstname" value="James" th:value="${currentUser.firstname}" />
From the documentation:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html
I did not have a form with input elements but only a button that should call a specific Spring Controller method and submit an ID of an animal in a list (so I had a list of anmials already showing on my page). I struggled some time to figure out how to submit this id in the form. Here is my solution:
So I started having a form with just one input field (that I would change to a hidden field in the end). In this case of course the id would be empty after submitting the form.
<form action="#" th:action="#{/greeting}" th:object="${animal}" method="post">
<p>Id: <input type="text" th:field="*{id}" /></p>
<p><input type="submit" value="Submit" /> </p>
</form>
The following did not throw an error but neither did it submit the animalIAlreadyShownOnPage's ID.
<form action="#" th:action="#{/greeting}" th:object="${animal}" method="post">
<p>Id: <input type="text" th:value="${animalIAlreadyShownOnPage.id}" /></p>
<p><input type="submit" value="Submit" /> </p>
</form>
In another post user's recommended the "th:attr" attribute, but it didn't work either.
This finally worked - I simply added the name element ("id" is a String attribute in the Animal POJO).
<form action="#" th:action="#{/greeting}" th:object="${animal}" method="post">
<p>Id: <input type="text" th:value="${animalIAlreadyShownOnPage.id}" name="id" /></p>
<p><input type="submit" value="Submit" /> </p>
</form>

How to set default value in thymeleaf th:field

I have a form and I want to set default value in the field below but it's not working.
<span>ID User:</span>
<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />
</div>
<div>
<span class="name-in">ID Room:</span>
<input type="text" th:value="${id}" th:field="*{room}" th:errorclass="field-error" />
</div>
I read some topic about this problem and I try to change as given below
th:attr="value = ${session.name}"
But It's still not working. Field ID User is empty. I don't know how to solve this problem.
Although your question contain less information, but i think you want to put default value for all field. If you like to do so change
`<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />`
to
<input type="text" name="userid" value="as your wish" th:errorclass="field-error" />
Instead of changing the html, you should instead set the value of *{userid} in your controller. That way you can keep your html the same:
// Controller
modelObject.setUserId(session.name);
// HTML
<input type="text" th:field="*{userid}" th:errorclass="field-error" />

Edit Form (Laravel 5.3)

I am using same form for Create and Edit i have a Folder field and i want to set its value Inbox by default however if a user want to replace it with own folder he /she can replace
<input type="text" name="folder" value="INBOX {{isset($mbl->folder) ? $mbl->folder : '' }}" class="form-control" />
when i go on edit it shown **INBOX INBOX
I want that Default value should not be show in Edit form it just show the name from Folder remember that i can't remove value="INBOX" because i need it i am using it as default value and i am using same form for Create and Edit
Thanks For Help
You Need to pass default value as
<input type="text" name="folder" value="{{isset($mbl->folder) ? $mbl->folder : 'INBOX' }}" class="form-control" />
Try This hope it will work
Simply use it as follows:
<input type="text" name="folder" value="{{$mbl->folder or 'INBOX' }}" class="form-control" />
More info here.

Adding value in input text field using Rails 3

I have a small problem.I want to add value inside the input text field which is fetching from DB.
Suppose i have a input field like below.
<input type="text" class="form-control" placeholder="Receipt No">
I have to add #user.Receipt_No inside it as its value.This value is fetching from user table of DB.Please help me resolve this problem.
Try this,
<input type="text" class="form-control" placeholder="Receipt No" value="<%= #user.Receipt_No %>">

Resources