mtownsend
11/15/2018 - 9:36 PM

AlphaSpacesDashes

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

/**
 * Permit values with alpha characters, spaces, and dashes.
 */
class AlphaSpacesDashes implements Rule
{
    public $attribute;

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct($attribute = null)
    {
        $this->attribute = $attribute ?? ':attribute';
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return preg_match("/^[\pL\s\-]+$/u", $value);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return "{$this->attribute} may only contain letters, spaces, and dashes.";
    }
}