5

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? Commented Oct 3, 2017 at 9:35
  • @nikita Ill update my question Commented Oct 3, 2017 at 9:35
  • @HerrWalter Can you paste the code snippet for the validation with the request object ? Commented Oct 3, 2017 at 9:51
  • @PSJ yes, i will in a moment. Allmost out of meeting Commented Oct 3, 2017 at 10:33
  • @PSJ I've updated my question. Commented Oct 3, 2017 at 11:49

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

this doesn't work for me, the fields aren't validated this way. I updated my post with the full Request object.
replace string with alpha where ever you use and also change alpha:max:255 to alpha|max:255 @HerrWalter
0

try this

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

1 Comment

this doesn't work for me, the fields aren't validated this way. I updated my post with the full Request object.
0

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',

5 Comments

instead of posting a new answer while keeping the old one, better to update the old answer and leave a comment there
When used like that, it still says that the field is required but I'm providing the text necessary
@HerrWalter i think you should try this way: 'general[achternaam]' => 'required|max:255',
@AmrinderSingh Thank you, i tried and it fails with the 'required' error
try this 'general.achternaam' => 'required|max:255 as array fields not validated like above and also make sure that whether two fields does not have same name in html.
0

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.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.