Home > Back-end >  Field 'invite' doesn't have a default value Laravel validation
Field 'invite' doesn't have a default value Laravel validation

Time:01-05

Why does it give such an error? With required_if, I get true or false, and so do I get the string

    public function rules()
    {
        $import = new ServerInviteClient();
        $response = $import->client->request('GET', 'invite');
        $items = $response->getBody()->getContents();

        return [
            'id' => 'required|unique:App\Models\Server,id',
            'content' => 'required|string',
            'invite' => [
                new RequiredIf(str_contains($items, $this->id)),
                'exclude_unless:invite,false|required|string'
            ],
        ];
    }

CodePudding user response:

It looks like the issue is with this line: 'exclude_unless:invite,false|required|string'

The 'exclude_unless' rule means that the 'invite' field is only required if the value of 'invite' is not equal to 'false'. If the value of 'invite' is 'false', then this rule will be skipped and the 'required' rule will not be applied.

If you want the 'invite' field to be required regardless of the value of 'invite', you should remove the 'exclude_unless' rule and just use the 'required' rule by itself.

CodePudding user response:

In my opinion, using Preparing Input is better than this way...

Set a private $isInvatied in class, Check invitation and get $items in preparing, and set as true or false on $this->isInvatied,

So in rule array due $this->isInvatied value use nullable || required rule...

  • Related