I am trying to add an extra validation rule that checks to ensure that a username is a word. I created a new rule (SingleWord) like so:
public function passes($attribute, $value)
{
$dd = strpos(trim($value), ' ');
if($dd !== false){
return false;
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Username must be one word';
}
I now added this rule to the validator like so:
$validator = Validator::make($data, [
'name' => 'required|string|max:255|unique:merchants',
'email' => 'required|string|email|max:255|unique:merchants',
'password' => 'required|string|min:6',
'username' => 'required|string|unique:merchants',
'username' => [new SingleWord]
],[
'name.required' => "Company name is required",
]);
return $validator;
But the validator returns the error message even when I enter one word. Please what might be wrong here?
truefromfunction passes(). The default return value if you don't return something isnullwhich might be evaluating to false.