I have created a few new validation rules for my Laravel application by following the Laravel docs https://laravel.com/docs/5.6/validation#custom-validation-rules, however, when I attempt to register the rules into my custom requests rules array, an error is thrown:
#message: "trim() expects parameter 1 to be string, object given"
#code: 0
#file: "/Users/ari/Projects/dps/sites/acg/vendor/laravel/framework/src/Illuminate/Validation/ValidationRuleParser.php"
#line: 217
#severity: E_WARNING
My custom rule - NotContainsEmail.php:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class NotContainsEmail implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return (strpos($value, '@') !== false);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'This field cannot contain an email address.';
}
}
My Request - QuoteRespondRequest:
<?php namespace Client\Http\Requests\Quotes;
use App\Rules\NotContainsEmail;
use Client\Http\Requests\FormRequest;
class QuoteRespondRequest extends FormRequest
{
public function rules()
{
return [
'help' => ['string', 'nullable', new NotContainsEmail],
'description' => ['string', 'nullable', new NotContainsEmail],
'community' => ['string', 'nullable', new NotContainsEmail],
'funding' => ['string', new NotContainsEmail],
];
}
}
I'm confused by the error, as the Laravel documentation clearly states that I can pass through an object, but Illuminate\Validation\ValidationRuleParser.php@217
clearly cannot handle objects.
Where have I gone wrong?
new NotContainsEmail
ornew NotContainsEmail()
?? – Davitnew Uppercase
syntax. See laravel.com/docs/5.6/validation#custom-validation-rules – user7191988