First and foremost:
I did read both entries # laravel and stackoverflow
I am trying to validate an array containing input[text]. They are defined currently as:
<input type='text' name='user[0][name]'/><div>#error('user[0][name]'){{ $message }}#enderror</div>
<input type='text' name='user[1][name]'/><div>#error('user[1][name]'){{ $message }}#enderror</div>
I tried the 3 variants below as well:
<input type='text' name='user[][name]'/>
<input type='text' name='name[]'/>
<input type='text' name='name[0]'/>
My ExampleController does this, in the store() method:
$validator = Validator::make($request->all(), [
'user.*.name' => 'required|string',
])->validate();
I've also tried using:
$validatedData = $request->validate([
"user.*.name" => "required|string",
]);//*/
The other option that I've tried to use to match was (for the other case):
'name.*' => 'required|string',
None of these manage to print an error message in the div that follows the input.
The only way for me to get to see the error, is if I do the validation in of the two below (for each case):
"user[0][name]" => "required|string",
"name[0]" => "required|string",
So... what is it that I'm doing wrong?
user[0][name] is the correct syntax for naming your form input elements.
To access the error message(s), however, use dot notation: user.0.name.
Here's a working playground.
Related
I have a Request file to validate the form. What I am encountering is that, I have at least 24 form fields to validate, but the problem is fields get validated and return back to the view, when validation fails. But the errors is not get sent to view.
But, I mentioned the rules for lets say only 6 fields, then the message is being displayed properly in the view.
I have done the research on stackoverflow, and tried every solutions, but none work for me.
use this type of dorm fields in your view
<div class="col-8 ml-auto">
<input id="serialNumber" type="text" class="form-control t-cap"
name="serialNumber" value="{{ $invoice->serialNumber }}">
</div>
#if ($errors->has('serialNumber'))
<span class="col-md-12 form-error-message">
<small for="serialNumber">{{ $errors->first('serialNumber') }}</small>
</span>
#endif
</div>
And in Your Controller use Like This
$rules = array(
'customer.mobile'=> 'required|regex:/\+91[[:space:]]\d{10}/',
'serialNumber' => 'required',
);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return Response::json(array(
'status' => 0,
'errors' => $validator->errors()
), 400);
}
I'm studying Laravel and made contact form.
my Laravel Framework is 7.15.0
I wrote this code to saving form values useing old function.
<input class="form-check-input" type="radio" name="q1[]"
value="A" #if(is_array(old('q1')) && in_array("A", old('q1'))) checked #endif>
<label class="form-check-label" >A</label>
<input class="form-check-input" type="radio" name="q1[]"
value="B" #if(is_array(old('q1')) && in_array("B", old('q1'))) checked #endif>
<label class="form-check-label" >B</label>
However after I wrote this I couldn't store data to my mysql.
Here is error message.
ErrorException
Array to string conversion
vendor/laravel/framework/src/Illuminate/Support/Str.php:449
Seems like saving array data to my mysql is problem I guess.
Here is my controller code.
Could someone teach me how to write store data as not array please?
public function ContactUsForm(Request $request) {
// Form validation
$this->validate($request, [
'name' => 'required',
]);
Contact::create($request->all());
\Mail::send('mail', array(
'name' => $request->get('name'),
'q1_s' => $request->get('q1')
), function($message) use ($request){
$message->from($request->email);
$message->to('mail#myemailadress.com', 'you got mail ')->subject($request->get('you got mail'));
});
I want to validate input filed which allow entering numbers only and if numeric, alphabet or copy paste not allowed and show the alert message.
<input id="amount" name="amount" type="text" placeholder="Amount" class="form-control"></div>
This my input field.
I search on google but not getting a proper answer.
From the client side you might want to create an input that already validates, to prevent unnecissary requests
<input type="text" name="amount" pattern="[0-9]*" title="Numbers only">
or
<input type="number" name="amount" title="Numbers only">
Next you want to validate the same on the server, to protect your request.
public function store(Request $request)
{
$validatedData = $request->validate([
'amount' => 'required|nummeric',
]);
// do something with amount
}
Do a server side validation as below
In your model
public static function rules()
{
return [
'amount' => ['required','numeric'],
];
}
In your controller write below code
$validator = Validator::make($request->all(), Your ModelName::rules());
if ($validator->fails()){
//display error msg
}
When registering a new user, I use a RegisterFormRequest from Laravel.
How can I use the label name translations to display correctly the error message ?
Here is my html code,
<div>
<label for="password">
{{ Lang::get('form.password') }} *
</label>
<input name="password" type="password" required>
</div>
<div>
<label for="password_confirmation">
{{ Lang::get('form.password.confirm') }} *
</label>
<input name="password_confirmation" type="password" class="form-control" required>
</div>
Here is what is displayed. Note that the input field's "password" is used as is, but not any translations.
in the Http\Controllers\YourController.php
public function postRegister(RegisterFormRequest $request)
{
$validator = $this->registrar->validator($request->all());
$validator = $validator->setAttributeNames( [
'password' => 'My Password Name'
] );
will do the trick.
As #ali as implied, it might be better to put these values into your request class (it is where you can put the specific rules, so why not the specific label translation?).
in the Http\Controllers\YourController.php
public function postRegister(RegisterFormRequest $request)
{
$validator = $this->registrar->validator($request->all());
$validator = $validator->setAttributeNames( $request->messages() );
in the Http\Requests\RegisterFormRequest.php
public function messages()
{
return [
'password' => Lang::get('form.password'),
Laravel validation attributes "nice names"
And (as #mohamed-kawsara mentionned) in
resources/lang/de/validation.php
return [
...
"confirmed" => "The :attribute confirmation does not match.",
...
What you need is custom message in you request class.
Write this function in your request class.
public function messages()
{
return [
'password.required' => 'YOUR CUSTOM MESSAGE FOR PASSWORD REQUIREMENT'
];
}
I am wondering if anyone knows how I can do this.
Currently, I am using form request validation so my store method looks something like
public function store(ProfileStore $request)
{
// do stuff.
Input::flush();
return redirect()->back();
}
^ Note the input flush, I don't want certain input stored as "old input" or passed back to the form so I am flushing it.
and then in my ProfileStore I have a some basic validation (eg.
public function rules()
{
return [
'title' => 'required|max:255',
'body' => 'required',
];
}
The problem is when I use Request Validation, its passing the the input back into the form along with the error messages. I have tried flushing input from the validation file, but doesn't work.
If I manually create a validator from my store method and not use Request Validation it works fine and will not pass back input.
Update:
So I am using Laravel Collective Forms & HTML, I think its related to that. Which is weird because I am using Form::open and as far as I know only Form model binding should be doing this on Form::model..
If I remove
Form::text('testfield', null);
and replace with standard input
<input tpye="text" name="testfield" value="">
No input is returned after validation which is correct. However when using Form::input values are returned from validation.
{!! Form::open(['route' => ['frontend.account.profile.save'], 'id' => 'profile-update', 'class' => 'profile', 'role' => 'form']) !!}
<div class="form-body">
<div class="row" id="billing_details_div">
<div class="col-xs-12">
{{-- Input gets passed back after failed validation when using Form::text() --}}
Form::text('testfield', null);
{{-- No Input gets passed back when using stantard HTML input
<input tpye="text" name="testfield" value=""> --}}
</div>
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-lg btn-success"><span class="glyphicon glyphicon-floppy-disk"></span> Update Profile</button>
</div>
</div>
</div>
{!! Form::close() !!}
Any ideas?
Write this function in your ProfileStore request class and it should fix that.
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson()) {
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withErrors($errors, $this->errorBag);
}
I'm not quite sure if it works, so let me know if it does. ;)
Update
Try this as well
protected $dontFlash = ['password', 'password_confirmation', 'your-other-inputs'];