Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate field input is string laravel

In the documentation I saw this

string:value

The field under validation must be a string type.

But as I understand (from the scarce explanation + the example given) I must validate with a certain string value. But what I want is to validate simply that the input value contains just letters from the alphabet.

Apparently

'name'  => 'required|string'

won't cut it, because it has to be string:SomeValue

So, what is the correct way, to set a validation rule In the model class that validates against string?

like image 382
Milkncookiez Avatar asked Feb 26 '15 23:02

Milkncookiez


2 Answers

I think what you're looking for is the 'alpha' validation rule Laravel provides. This will ensure the value you are validating contains only alphabetic characters. Docs: http://laravel.com/docs/4.2/validation#available-validation-rules

like image 174
IndianAg0711 Avatar answered Nov 17 '22 00:11

IndianAg0711


You can use the regex validator for this:

'name'  => ['required', 'regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/']

([a-zA-Z]+): This means that the input must begin with one or more letters.

(\s[a-zA-Z]+)*: This means that after the first word (name) you can add one space with another word (name) & it can be done 0 or more times.

If you want to force the user to input his first name(s) + last name(s) you could change the '*' character to a '+' character.

This might be a late answer but it still might help someone.

like image 38
Robert Kujawa Avatar answered Nov 17 '22 01:11

Robert Kujawa