0

I have a validator that always passes.I need it to fail if username has less than 3 characters.

Here are my routes:

Route::get('/', function()
{
return View::make('validation.form');
 });

 Route::post('registration', function()
{
// Fetch all request data.
 $data = Input::all();
 var_dump($data);
// Build the validation constraint set.
 $rules = array(
 'username' => 'alpha_num|min:3' //or username =>array('alpha_num','min:3')
 );
var_dump($rules);
// Create a new validator instance.
$validator = Validator::make($data, $rules);

if ($validator->passes()) {
// Normally we would do something with the data.
return 'Data was saved.';
}


return 'data not saved';

});

And bellow is 'my' form

{{ Form::open(array('url' => 'registration')) }}

{{-- Username field. ------------------------}}
{{ Form::label('username', 'Username') }}
{{ Form::text('username') }}
<br/>

{{-- Email address field. -------------------}}
{{ Form::label('email', 'Email address') }}
{{ Form::email('email') }}
<br/>


{{-- Password field. ------------------------}}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
<br/>


{{-- Password confirmation field. -----------}}
{{ Form::label('password_confirmation', 'Password confirmation') }}
 {{ Form::password('password_confirmation') }}
<br/>


{{-- Form submit button. --------------------}}
{{ Form::submit('Register') }}


{{ Form::close() }}

When I click the submit button I am always routed to /registration with the message data saved.Why does the validator not working..?Any idea

2
  • Can you add the var_dumps? Commented Nov 22, 2013 at 11:09
  • The data is successfully passed..i used required also and it failed with empty input..but shouldn t min:3 make it fail.? Commented Nov 22, 2013 at 11:31

2 Answers 2

1

Require rule checks whether the input is filled or not..so if you give an empty input without require rule the validator ignores min:3 and it successes..but if you give two chars as input it will fail.so in order to avoid empty data use require rule..

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

1 Comment

I think you nailed it
0
'username' =>  = 'required|alpha_num|min:3|max:15';

1 Comment

pls add a explanation for your solution - stackoverflow.com/help/how-to-answer

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.