Monday, 26 August 2013

How to make Laravel's Validator $rules optional

How to make Laravel's Validator $rules optional

Let's say I have User model with two methods:
User.php
class User extends Eloquent
{
/* Validation rules */
private static $rules = array(
'user' => 'unique:users|required|alpha_num',
'email' => 'required|email'
);
/* Validate against registration form */
public static function register($data)
{
$validator = Validator::make($data, static::$rules);
if($validator->fails())
{
/*... do someting */
}
else
{
/* .. do something else */
}
}
/* Validate against update form */
public static function update($data)
{
$validator = Validator::make($data, static::$rules);
if($validator->fails())
{
/*... do someting */
}
else
{
/* .. do something else */
}
}
}
My question: How can I make validation rules optional, so even if data for
update() would be just email field, it would ignore user and still
validate to true.
Is this even possible or am I missing something?
Sorry for my bad english.
Thanks

No comments:

Post a Comment