Validation errors message on array - php

Quoting laravel documentation:
Likewise, you may use the * character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:
'custom' => ['person.*.email' => ['unique' => 'Each person must have a unique e-mail address']]
It seems it does not work. I have a validation message:
'infos.*.*.*' => ['required' => 'My text']
Then I have some inputs in my view:
<input type="text" name="infos[1234][0][name]">
<input type="text" name="infos[1234][1][name]">
<input type="text" name="infos[5678][0][name]">
And in my controller I validate the input:
$this->validate($request, [
'infos.*.*.*' => 'required'
]);
And in view I have a error displayer:
#if (count($errors) > 0)
<strong>Oops. Errors:</strong>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
And if I leave all input empty I got:
My text
My text
My text
My text
My text
etc.
What's wrong with my code? Reading Laravel documentation I though it should have worked (I mean: it should have been displayed only once). Did I misunderstood something?

This is working as intended. Since you're passing multiple inputs in an array, the validation throws an error for each item. Therefore 3 inputs with 2 causing errors and 1 passing would obviously pass error for 2 inputs. So in your case the multiple errors are due to multiple inputs failing validation.

Related

Array as input in Laravel Form (Submit funcionality not working)

I am building a form in Laravel, which receives dynamic input. The user can add a link in the input text, but can choose to add more links as desired. The funcionality for that was developing using jquery. The problem is that when I try to save the form (submit), I get this error message.
ErrorException in helpers.php line 531:
htmlentities() expects parameter 1 to be string, array given (View: C:\Users\jkabbas\Documents\GitProects\rastro\src\resources\views\arquiteturas\novo.blade.php)
This is the code excerpt in the laravel page, which is causing the problem. When I remove the square brackets [] from name="txt-link[]", the error message is gone, but I need to use array because I am dealing with multiple inputs.
<td>
<input type="text" placeholder="Digite o link da arquitetura" class="form-control" id="txt-link" name="txt-link[]" value="{{old('txt-link')}}" style="width: 730px;">
#if($errors->has('txt-link'))
#foreach ($errors->get('txt-link') as $message)
<span class="help-block" style="margin-top:5px; margin-bottom:-5px; color:rgb(170, 56, 56)">
<b>{{ $message }}</b>
</span>
#endforeach
#endif
</td>
Backend code
function store(Request $request) {
$this->validate($request, [
'combo_produto'=>['not_in:0'],
'combo_projeto'=>['not_in:0'],
'txt-link[]'=>['required'],
]);
$save_arquitetura = Arquitetura::create([
'produto_id' =>$request['combo_produto'],
'projeto_id' =>$request['combo_projeto'],
]);
return redirect('/arquiteturas')->with('msg_success', 'Dados salvos com sucesso!');
}
this is caused by value="{{old('txt-link')}}". Some kind of error occurred in your backend while you are uploading multiple files and your return all old input but you are not handling it here value="{{old('txt-link')}}. thats is the reason behind htmlentities() expects parameter 1 to be string, array given

Laravel array validation dont show error message

Firstly I can say that after search I dont find any solution about this. I do validation array like this post: laravel validation array
I need validate each size array position. I write this validation code:
// Fields validation
$request->validate([
'name' => 'required|max:150',
'theySay' => 'nullable|array',
'theySay.*' => 'string|max:1000',
'theyDontSay' => 'nullable|array',
'theyDontSay.*' => 'string|max:1000',
]
Where theySay and theyDontSay are both array of strings. In migration I have both fields (text) like strings of 1000 characters.
$table->string('text', 1000);
And validation works correctly. I mean, if put a text greater than 1000 chars I cannot save but..dont show any error message.
I want the error message to be shown in the input just like the rest of the fields.
What am I doing wrong?
Best regards
'YOUR_FIELD' => '...|...|max:1000| ...'
Look at the Laravel validation docs for more information
Please put below code in your blade file for show any error message.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

Laravel validation min and regex being outputted to the same message

I'm having a bit of a weird issue and i'm not quite sure why its happening or if i'm just missing something.
I have multiple validation rules on a input 'postcode' field, like so
'postcode' => [
'required',
'min:5',
'max:8',
'regex:/^[a-z]/i'
],
And i've also wrote custom messages for the rules like so
return [
'postcode.min' => 'The postcode must be at least 5 characters',
'postcode.regex' => 'Sorry, the postcode must start with a letter',
];
All of my errors are being looped and displayed like this
<div class="error-block {{ (count($errors) > 0) ? '' : 'hide' }}">
<div class="col-12">
<ul>
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
#endif
</ul>
</div>
</div>
But when both rules are hit for the postcode, for example with the input '123', then it pushes both errors out on the same line, but all other errors show correctly, like this
The postcode must be at least 5 characters,Sorry, the postcode must start with a letter.
The username field is required.
The address line 1 field is required.
Why is laravel pushing the min and regex rule messages out on the same line?
As described in the docs, validation rules should be expressed as a string, with different rules for a single input separated by a pipe |. The set of inputs to be validated should be an array, but not the rules themselves.
In your case, for the postcode input:
'postcode' => 'required|min:5|max:8|regex:/^[a-z]/i',
That means your $error is an array. if you want to list all error as new line, check if the error is also an array or is not an array.
#if(count($errors) > 0)
#foreach($errors->all() as $error)
#if(is_string($error))
<li>{{$error}}</li>
#else
#foreach( $error as $newBreakdown)
<li>{{$newBreakdown}}</li>
#endforeach
#endif
#endforeach
#endif

validator rules for a database select / dropdownmenu (in a form) laravel 4.1

I have a problem with laravel, and my form.
In my form (createBand.blade.php), i made a form with a dropdownlist wich call a database table: musicalGenre.
It should be noted that the dropdownmenu/list (select form) calls to another table in the database called MusicalGenre, and the form where I want the dropdownlist is to register a band in the database table Bands.
The select form works, i could choose in a dropdownlist all musicalGenre_name in my table after seeding them. Here's the blade page code (of course i have open, close the form and the section like laravel requires):
<p>
{{Form::label('name','Name of the Band: ')}}
{{Form::text('name',Input::old('name'))}}
</p>
#if ($errors->has('name'))
<p class='error'> {{ $errors->first('name')}}</p>
#endif
<br>
<br>
<p>
{{Form::label('email','Band Email: ')}}
{{Form::text('email',Input::old('email'))}}
</p>
#if ($errors->has('email'))
<p class='error'> {{ $errors->first('email')}}</p>
#endif
<br>
<br>
<p>
{{Form::label('musicalGenre','your style: ')}}
{{Form::select('musicalGenre_name', $genre_options = array('' => 'select your style') + musicalGenre::lists('name'), Input::old('musicalGenre_name')) }}
</p>
#if ($errors->has('musicalGenre'))
<p class='error'> {{ $errors->first('musicalGenre_name')}}</p>
#endif
<br>
<br>
I have a controller named createBandController, where i made some rules for validators for the blade form. The poblem is:
i can't pass the validators, that is to say, even if i choose a musical genre in my dropdownlist, for laravel there no choice made.
I have the error "musical genre is required". I don't understand the validator rules for a select form in my controller, or i don't know what i'm suposed to input in the rules for musical genre. Here's the controller code:
public function createBand() {
$result = MusicalGenre::all();
$genre = $result->lists('name');
$inputs = Input::all();
$rules = array(
'name' => 'required|between:1,64|unique:bands,name',
'email' => 'between:1,128|email|unique:bands,email',
'musicalGenre' => 'integer|required|in:musicalGenre'
);
$validation = Validator::make($inputs, $rules);
if ($validation->fails()) {
return Redirect::to('createBand')->withInput()->withErrors($validation)
->with('alert_error', 'you have some mistakes');
Don't pay attention to the names, (i'm french, i changed them in order to be clear for you), and i'm sure that is the validator of my dropdownlist who make problems when i fill out my form, because i can't pass it. In my original project code, there are no spelling mistakes.
All my validators and variable names work. I think i need to find the correct rules for a select form input and validators in order to laravel knows i made a choice when i choose in my dropdownlist.
At first i thought to specify that the dropdownlist use the database table musicalGenre. Like i specified that some fields are in the database table Bands, like this:
'name' => 'required|between:1,64|unique:bands,name'
Here, "name" is a field of the database table Bands. But it didn't work too.
If anyone have a solutions or wants to help, i'm interested.
Thank you (and sorry if my english seems so bad).
The validation rule in: on the field musicalGenre won't work the way that you have implemented it. It expects a comma delimited list of strings which the field value is scrubbed against.
For example if the field was 'Gender' the validation would be:
'gender' => 'in:male,female'
To validate against musicalGenre against a model you will need to write a custom Validator. See http://laravel.com/docs/validation#custom-validation-rules
I am currently writing a custom validator for this 'belongs_to' validation and when I have it working I'll post it here.
UPDATE
I have written a custom validation rule that should help. Firstly create a validators.php file and include it in global.php
require app_path().'/validators.php';
Within validators.php create the custom rule:
Validator::extend('exists_in', function($attribute, $value, $parameters)
{
$result = $parameters[0]::find($value);
return ($result == null) ? FALSE : TRUE;
});
In your validations you could now have:
'musicalGenre' => 'integer|required|exists_in:musicalGenre'
The exists_in validation takes a parameter of the Model Class name
And to give this validation an error message, add the following to the array in app/lang/en/validation.php:
"exists_in" => "The selected :attribute is invalid."
Does exists:musicalGenre instead of in:musicalGenre help?

Laravel alternative value for input if Input::old is empty

Is there any utility function in Laravel that allows you to give an alternative value for a specific input field if the old value is empty? Currently I have the following code:
<input type="text" class="form-control" id="title" name="title" value="{{ (!empty(Input::old('title'))) ? Input::old('title') : 'hey' }}">
But its not really that pretty. Any ideas?
use
Input::old('title', 'fallback value')
Yes! Don't use the input tag :)
If you use {{ Form you will get this, and much, much more!
{{ Form::text('email', null, array('class'=>'form-control', 'placeholder'=>'Email Address')) }}
Check out the docs here (http://laravel.com/docs/html & http://laravel.com/docs/requests) and you will notice that when the input is flashed to the session, this input box rendered by blade will automatically replace that "null" (the second parameter) with the flashed value from the session.
This removes the need to check for the old input or have any nasty if/else checks inside your template. Also, you no longer need to worry about any HTML code injections or XSS happening, because Form::text will ensure that the text is correctly converted to their HTML entities.
Where you are checking for errors, you should use a Laravel validator. Something similar to this:
protected function createUser(){
$rules = array(
'email'=>'required|email',
'password'=>'required|min:6|confirmed',
'password_confirmation'=>'required'
);
$validator = Validator::make(Input::all(), $rules);
if (! $validator->passes()) {
Input::flashExcept('password', 'password_confirmation');
return Redirect::to('my_form');
} else {
// do stuff with the form, it's all good
}
return Redirect::intended('/complete');
}
Additionally, in your template, you can show all of the errors from the form:
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
Or just select the first error and show it under the {{ Form::text
#if ($errors->has('first_name'))
<span class="error">{{$errors->first('first_name')}}</span>
#endif
Laravel has all of this built in, and you get it for free! Using Requests, Validators, Blade/HTML

Categories