Skip to content

Instantly share code, notes, and snippets.

@freekmurze
Last active February 15, 2018 17:29
Show Gist options
  • Save freekmurze/ceebdf22e7225d63ff10 to your computer and use it in GitHub Desktop.
Save freekmurze/ceebdf22e7225d63ff10 to your computer and use it in GitHub Desktop.
A handy validate function for Laravel 5
/**
 * Validate some data.
 *
 * @param string|array $fields
 * @param string|array $rules
 * @return bool
 */
function validate($fields, $rules)
{
    if (! is_array($fields)) {
        $fields = ['default' => $fields];
    }

    if (! is_array($rules)) {
        $rules = ['default' => $rules];
    }

    return Validator::make($fields, $rules)->passes();
}

All available validation rules in Laravel can be used.

Example:

validate('20150230', 'date'); // returns false
validate('20150228', 'date'); // returns true
validate('[email protected]', 'email'); // returns true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment