5
votes

I need some help for validating my form with Laravel 5.4.

My form:

{{Form::bsText('general[firstname]')}} 
{{Form::bsText('general[lastname]')}}

Then I have a RequestObject for the validation with the following rules:

'general[firstname]' => 'required|string:max:255',
'general[lastname]' => 'required|string:max:255',

This way it generates an error 'required' when not empty as expected. Though when i fill in a string, it still gives the required error message.

I've also tried the following as from the laravel docs:

'general.firstname' => 'required|string:max:255',
'general.lastname' => 'required|string:max:255',

and:

'general.*.firstname' => 'required|string:max:255',
'general.*.lastname' => 'required|string:max:255',

Both of the above don't give an error at all.

On request, here is my full Request object:

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    $user = Auth::user();

    return ($user && $user->isProjectManager()) ||
            ($user && $user->isAdmin());
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    switch($this->method()){
        case 'GET':
        case 'DELETE':
            return [];
        case 'POST':
            return [
                'name' => 'required|string|max:255',
                'email' => 'required|string|email|max:255|unique:users',
                'role' => 'in:2,3,4,5,6,7',
                'password' => 'required|string|min:6|confirmed',
                'project_manager_gegevens_photo' => 'required_if:role,2|mimes:png,jpeg,gif',
                'general[voornaam]' => 'required|alpha:max:255',
                'general[achternaam]' => 'required|string:max:255',
                'general[date]' => 'required_if:role,3,4,5|date|after:today',
                'general[telefoonnummer]' => 'required_if:role,3,4,5',
                'general[interne_medewerker]' => 'boolean',
                'general[geslacht]' => 'in:m,v,o',
            ];
        case 'PUT':
        case 'PATCH':
            return [
                'name' => 'required|string|max:255',
                'password' => 'required|string|min:6|confirmed',
            ];
        default:return [];
    }
}

Proof that it is has something to do with array validation: When I change the name to:

{{Form::bsText('general_firstname')}} 

and

'general_firstname' => 'required|string:max:255'

It validates like it should en what you'd expect. Though, i like things clean and seperate and want a array that hold all of the general fields.

So, how can I validate it so it is an array?

5
Can u elaborate it your issue?nikita
@nikita Ill update my questionHerrWalter
@HerrWalter Can you paste the code snippet for the validation with the request object ?Pubudu Jayawardana
@PSJ yes, i will in a moment. Allmost out of meetingHerrWalter
@PSJ I've updated my question.HerrWalter

5 Answers

1
votes

Use alpha insteadof string,

like,

'general.firstname' => 'required|alpha|max:255'

Replace this,

'general[firstname]' => 'required|string:max:255',

Refer Docs, https://laravel.com/docs/5.4/validation#rule-alpha

0
votes

try this

'general.firstname' => 'required|alpha|max:255',
'general.lastname' => 'required|alpha|max:255',
0
votes

Are you using it like this?

 'general[voornaam]' => 'required|alpha:max:255',
  'general[achternaam]' => 'required|string:max:255',

if so can u please replace it with this:-

'general[voornaam]' => 'required|alpha|max:255',
'general[achternaam]' => 'required|alpha|max:255',
0
votes

Well, i haven't checked what changes have been made in 5.4 but as per 5.1 i did the following in my project for form validations:

    $data = Input::all();

    $rules = [
            'fname' => ['required', 'min:3'],
            'lname' => ['required', 'min:3'],   
            'email' => ['required', 'email', 'unique:users', 'min:3'],
            'password' => ['required', 'min:4'],
            'type' => ['required']
            ];
    $validator = $this->validate($request, $rules);

you can see for fname and lname i have not mentioned any data type, i.e the default is string, so you can try this way i think.

may be this can help you.

0
votes

So, what I forgot was the use of custom form components. In these components i looked for the name of the field. So i would check

$errors->get('general[voornaam]')

Where I should get them with:

$errors->get('general.voornaam')

Yeah! Enough for today!