Repopulate input fields when validation fails in update a row Laravel - laravel-5

Is there any way to repopulate input fields when validation fails in update any row? I know I can do it with laravel form model binding, but i dont want to use it. I can populate form from database table when update row. I want to repopulate the form with old input when validation fails in update not with database value. Thanks in Advance

You can leverage the old() helper. It is used this way:
<input type="text" name="username" value="<?= old('username', 'default value') ?>">

Related

laravel form validation not working correctly with x-show alpine js

I have a situation where I am showing/hiding form fields based on the input of a previous field. For example:
<x-forms.select #change="bussingRequired = $event.target.value" label="Bussing Required" name="bussing_required" required >
<option value="Yes">Yes</option>
<option value="No">No</option>
</x-forms.select>
<div class="col-span-12" x-show="bussingRequired == 'Yes'">
<div>
<!-- show another required field -->
</div>
</div>
The problem with this is x-show only hides the element with CSS and the field still exists in the DOM. Since the hidden field is required, frontend validation fails.
My question is what is the better way to handle this? Is there a way to completely remove the element from the DOM with alpineJS? Is there a way to modify the frontend validation in laravel with something like a required_if like we have in the backend?

How to populate form after validation fails using custom formRequest class in laravel

I am using Custom ForRequest class to validate my form, it works fine. But when it redirects back on form page, the form is not populated.
I don't want to use
old('form_field_name')
My code will be weird if I use old('form_field_name') because I use same form to store and to update the model
What am I missing?
thanks
if you use the same form for store and update, use old with optional. just like
<input name="name" id="name" placeholder="Name" type="text" class="form-control" value="{{ old('name', optional($object)->name) }}">
this is the way you can get the old form values as well as the object values. if there is old value it will put that as value otherwise it will put the object value as value.
make sure to send the object as null from the create function.

How to hide parameter from url, when need to pass data from one page to another?

Instead of showing ../product/1
$breadcrumbs->push($name,url('/product/'.$id));
I want it to be displayed as ../product
Any method can be used to do this ?
Use hidden input field like:
<input name="id" value="{{$id}}" hidden>
Not a too good practice, But you can hide id parameter from url

why does Razor put validation attributes in #Html.Hiddenfor() helper?

it doesn't make any sense, is there anyway to make this not to act this way?
for
#Html.HiddenFor(model=>model.Id)
I get
<input type="hidden" value="e62fceab-588c-4777-bfe9-8516425a5028" name="Id" id="Id" data-val-required="The Id field is required." data-val="true">
MVC is automatically adding required validation to all non null-able fields. If you don't like this then you can make your id null-able.
It's just an additional layer of server side protection. It's trivial to change an outgoing hidden input with a man in the middle tool like Fiddler.
As for making it optional, there's almost surely a data attribute for that. Alternatively, adding a question mark after the property name in your model should do it.

How to make a field required without data annotation

I am using the MvcContrib Grid to display a table on the page. I am using a custom column to produce a checkbox on the table so that the user can select multiple rows and submit the form.
I don't want the form to submit unless at least one checkbox has been selected. I could easily write this Javascript myself to enforce the validation, but I wanted to know how I could fit it in with the unobtrusive library supplied with MVC3.
I imagine I just need to set my inputs with the proper classes and attributes and then the scripts (validate and validate.unobtrusive) on the page should pick them up and mark them as needing validation, but I haven't been able to get the right combination thus far.
Here is the input that I am currently generating:
<input type="checkbox"
name="foo"
value="#item.foo"
class="input-validation-error"
data-val-required="Please select an option."
data-val="true" />
Try setting the data-val attributes on the item, then you have to tell jQuery you have new content to re-parse the form via something like:
$.validator.unobtrusive.parse($('#yourForm'));
where form is of course a reference to your form element.
There is also this great posting and jQuery has a few internal adapters you can call:
from http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2
jQuery.validator.unobtrusive.adapters.addSingleVal("notequalto", "otherproperty", "mynotequaltofunction")
From my experience, a commonly overlooked mistake with displaying client-side validation is putting a Html.ValidationMessageFor(lambda) on the page.
Without that, no client-side validation will fire to prevent the form submit and/or display the message that is generated using annotations on the client-side.
Hope this helps.
<div class="editor-field">
<input class="text-box single-line"
data-val="true" data-val-number="The field Qty Available must be a number."
data-val-range="The field Qty Available must be between 0 and 120."
data-val-range-max="120" data-val-range-min="0"
data-val-required="The Qty Available field is required."
id="QtyOnHand" name="QtyOnHand" type="text" value="12" />
<span class="field-validation-valid" data-valmsg-for="QtyOnHand"
data-valmsg-replace="true"></span>
</div>
The tie-in between the data model annotations and the data-val-* attributes should be clear after reading the above code, but it's where the client side validation ties in might not be so obvious. Open the \Scripts\jquery.validate.unobtrusive.js file and search for "data-val". Right away you'll see that the JavaScript uses the data-val-, input- and field-* CSS classes to display/hide validation messages on the client.
The above is taken from this great article, which you might want to read in full.

Resources