Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

For example, the user pass the userName, email to me, and I would like to have a custom validation for check the DB's user table have a column with the both userName equal and email equal and status equal 1 or not? How the customised validation implements?

For example:

User input:

userName: Peter

email: [email protected]

In case 1, in the DB's user table:

Success: userName: Peter , email: [email protected], status: 1
Fail: userName: Peter , email: [email protected], status: 0
Fail: userName: Mary , email: [email protected], status: 1
Fail: userName: Peter , email: [email protected], status: 1
share|improve this question

2 Answers

up vote 3 down vote accepted

You can create a custom validation method as a catch all. The major problem here is that the validation extension will only ever pass the single attribute to the method rather than the values of all three. This will require you to hack up the validation. This method will be very bespoke to your particular application due to the hard coded nature of the table, column names and input. It also does not give you any way of telling which field the issue is with and would require some additional rule. Another suggestion would be to actually extend the validator class as a library to provide you with a much finer tuned validation engine for this circumstance.

Validator::register('usercheck', function($attribute, $value, $parameters)
{
    $count = DB::table('user')
      ->where('userName', '=', Input::get('userName'))
      ->where('email', '=', Input::get('email'))
      ->where('status', '=', Input::get('status'))
      ->count();

    return $count > 0;
});

To use it just add it as a rule... bear in mind this feels a bit hacky and there ARE better ways of doing this, most notably the method I suggested in the opening paragraph.

$rules = array(
    'userName' => 'usercheck'
);
share|improve this answer

You can use this validation.

'userName' => 'unique:user,username',
'email' => 'unique:user,email'

See the docs about this http://laravel.com/docs/validation#rule-unique (Laravel 3) or the Laravel 4 docs at http://four.laravel.com/docs/validation#rule-unique

share|improve this answer
But the problem will occur a problem if the db have these two data:userName: Mary , email: [email protected], status: 1, and userName: Peter , email: [email protected], status: 1. – Ted Wong May 7 at 12:08
1  
This would be my preferred, but it does not solve the "status=1" requirement. – Antonio Carlos Ribeiro May 7 at 14:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.