Slinava Surla
Slinava Surla

Reputation: 117

Laravel 4 alpha_dash validation odd issue

Laravel 4, validation. The following passes validation even though the username input field contains invalid characters of "đšđšžšđčč". Why is that? Should not alpha_dash allow only '/^([a-z0-9_-])+$/i'?

public function postSignup()
{
    $data = Input::all();

    $validator = Validator::make(
        $data,
        array('username' => 'required|alpha_dash')
    );

    if ($validator->fails())
    {
        echo 'fail';
    }
    else
    {
        echo 'ok';
    }

    return;
}

Upvotes: 0

Views: 2611

Answers (2)

Aken Roberts
Aken Roberts

Reputation: 13467

What you're experiencing is acceptable behavior for the Validator class. Here's the rule's exact code (at the time of this writing):

return preg_match('/^[\pL\pN_-]+$/u', $value);

The u modifier at the end enables UTF-8 support. \pL and \pN are then unicode character properties which match letters and numbers respectively (similar to \w and \d that you may already be used to).

I don't know exactly what data you're validating against, but UTF-8 support is a good thing to have in your application. That said, if you'd like to create your own rule that is more tailored, Validator::extend() is perfect for it.

Upvotes: 0

The Alpha
The Alpha

Reputation: 146191

Not sure about this but you may register a custom validation rule to allow only ascii characters

Validator::extend('asciiOnly', function($attribute, $value, $parameters)
{
    return !preg_match('/[^x00-x7F]/i', $value);
});

So, you can use something like this

array('username' => 'required|asciiOnly|alpha_dash');

It may work to disallow/invalidate non-ascii characters (not tested).

Upvotes: 3

Related Questions