laravel-validation

Laravel 5.3 Validation Fails when Variables are Null

怎甘沉沦 提交于 2019-12-04 16:46:54
问题 Since upgrading laravel from 5.1 to 5.3, I've got couple of odd issues with Validation. When I post a data like this: firstName null And the validation rules are like this: $validator = Validator::make($postData, [ 'firstName' => 'string|max:255', 'lastName' => 'string|max:255' ]); The above fails with the messages something like "The XYZ must be a string." . What I don't understand is: Why is the validation failing when it is not set as required ? Meaning, it should ignore it and not throw

Validate only alphanumeric characters in Laravel

爷,独闯天下 提交于 2019-12-04 11:06:37
问题 I have the following code in my Laravel 5 app: public function store(Request $request){ $this->validate($request, ['filename' => 'regex:[a-zA-Z0-9_\-]']); } My intentions are to permit filenames with only alphanumeric characters, dashes and underscores within them. However, my regex is not working, it fails even on a single letter. What am I doing wrong? 回答1: You need to make sure the pattern matches the whole input string. Also, the alphanumeric and an underscore symbols can be matched with

Custom Laravel validation messages

隐身守侯 提交于 2019-12-04 08:05:00
问题 I'm trying to create customized messages for validation in Laravel 5. Here is what I have tried so far: $messages = [ 'required' => 'Harap bagian :attribute di isi.', 'unique' => ':attribute sudah digunakan', ]; $validator = Validator::make($request->all(), [ 'username' => array('required','unique:Userlogin,username'), 'password' => 'required', 'email' => array('required','unique:Userlogin,email'),$messages ]); if ($validator->fails()) { return redirect('/') ->withErrors($validator) // send

Pass old input after Form Request validation in laravel 5

旧街凉风 提交于 2019-12-04 00:07:52
if the validation fails it's not clear to me how i could pass the old input to fill again the form. I mean, I know how to pass the data when using the Validator class and redirecting after fail with the method withInput(), but I'm trying to learn how to use Form Requests provided in laravel 5. Thanks $username = Request::old('username'); or in view: {{ old('username') }} Read more: http://laravel.com/docs/5.0/requests#old-input You can redirect with old data like below with validation error return redirect()->back()->withErrors($validator)->withInput(); 来源: https://stackoverflow.com/questions

Laravel 5.3 Validation Fails when Variables are Null

耗尽温柔 提交于 2019-12-03 09:59:40
Since upgrading laravel from 5.1 to 5.3, I've got couple of odd issues with Validation. When I post a data like this: firstName null And the validation rules are like this: $validator = Validator::make($postData, [ 'firstName' => 'string|max:255', 'lastName' => 'string|max:255' ]); The above fails with the messages something like "The XYZ must be a string." . What I don't understand is: Why is the validation failing when it is not set as required ? Meaning, it should ignore it and not throw an error if the value is empty, right? Why does the validation fail if the value is set as null ? Why

Validate only alphanumeric characters in Laravel

余生长醉 提交于 2019-12-03 06:46:42
I have the following code in my Laravel 5 app: public function store(Request $request){ $this->validate($request, ['filename' => 'regex:[a-zA-Z0-9_\-]']); } My intentions are to permit filenames with only alphanumeric characters, dashes and underscores within them. However, my regex is not working, it fails even on a single letter. What am I doing wrong? You need to make sure the pattern matches the whole input string. Also, the alphanumeric and an underscore symbols can be matched with \w , so the regex itself can be considerably shortened. I suggest: 'regex:/^[\w-]*$/' Details : ^ - start of

Laravel 5 how to validate route parameters?

邮差的信 提交于 2019-12-03 04:49:53
问题 I want to validate the route parameters in the "form request" but don't know how to do it. Below is the code sample, I am trying with: Route // controller Server Route::group(['prefix' => 'server'], function(){ Route::get('checkToken/{token}',['as'=>'checkKey','uses'=> 'ServerController@checkToken']); }); Controller namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Http\Requests; class ServerController extends Controller { public

Laravel 5 how to validate route parameters?

折月煮酒 提交于 2019-12-02 18:03:17
I want to validate the route parameters in the "form request" but don't know how to do it. Below is the code sample, I am trying with: Route // controller Server Route::group(['prefix' => 'server'], function(){ Route::get('checkToken/{token}',['as'=>'checkKey','uses'=> 'ServerController@checkToken']); }); Controller namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Http\Requests; class ServerController extends Controller { public function checkToken( \App\Http\Requests\CheckTokenServerRequest $request) // OT: - why I have to set full path

Laravel 4 Form Validation should be unique on update form, but not if current

强颜欢笑 提交于 2019-12-02 17:11:00
问题 I am trying to validate an update user profile form, whereby the validation should check that the email doesn't exist already, but disregard if the users existing email remains. However, this continues to return validation error message 'This email has already been taken'. I'm really unsure where I'm going wrong. Otherwise, the update form works and updates perfectly. HTML {{ Form::text('email', Input::old('email', $user->email), array('id' => 'email', 'placeholder' => 'email', 'class' =>

Laravel Validation: required_with_all condition always passing

纵饮孤独 提交于 2019-12-01 22:42:10
As per laravel validation documentation : required_with_all:foo,bar,... The field under validation must be present only if all of the other specified fields are present. Here is my test: Route::get('/test/{param}', function($param) { $screenRules = array( 'foo' => 'string', 'param' => 'required_with_all:foo', ); $validator = Validator::make(array('param' => $param), $screenRules); if($validator->fails()) { echo '<pre>'; print_r($validator->errors()); echo '</pre>'; die('Dying now!'); } else { echo 'All Good!'; die('Dying now!'); } }); I would expect that since I am not passing foo , this test