requiredWithout validation

I am trying to run a requiredWithout validation rule, I want contact number to be required if email is null. The issue I am having is that contact number is inside the schema of a fieldset that is from the profile model. Where as email is on user directly.
Forms\Components\TextInput::make('contact_number')
->unique(app(Profile::class)->getTable(), 'contact_number', $form->getRecord()->profile ?? null)
->requiredWithout(function ($form) {
// return $form->getRecord()->email;
return $form->getComponent('email')->getValue();
})
->tel()
->maxLength(255),
Forms\Components\TextInput::make('contact_number')
->unique(app(Profile::class)->getTable(), 'contact_number', $form->getRecord()->profile ?? null)
->requiredWithout(function ($form) {
// return $form->getRecord()->email;
return $form->getComponent('email')->getValue();
})
->tel()
->maxLength(255),
This is my current attempt. But getting this error: An attempt was made to evaluate a closure for [Filament\Forms\Components\TextInput], but [$form] was unresolvable. If I just have the requiredWithout('email') the validation is looking for data.profile.email which is incorrect as email isn't in profile, and if I call user.email its then searching data.profile.user.email
1 Reply
Jamie Cee
Jamie Cee7mo ago
if I have it as requiredWithout($form->getComponent()) that also wont work, as getComponents is an empty array? Also tried Get, but 'email' returns null. Model is no use as that is likely to only check for a value on the model relation rather than the input field? Anyone have any ideas what im missing?
Forms\Components\TextInput::make('contact_number')
->unique(app(Profile::class)->getTable(), 'contact_number', $form->getRecord()->profile ?? null)
->requiredWithout(fn () => dd($form->getComponent('email')->getStatePath()))
->tel()
->maxLength(255)
Forms\Components\TextInput::make('contact_number')
->unique(app(Profile::class)->getTable(), 'contact_number', $form->getRecord()->profile ?? null)
->requiredWithout(fn () => dd($form->getComponent('email')->getStatePath()))
->tel()
->maxLength(255)
SO once again, it must be the path, so state path is data.email. But its giving "required when data.profile.data.email". So how can I tell it to look outside of this relationship fieldset?
Fieldset::make('Profile')
->relationship('profile')
->schema([
Forms\Components\TextInput::make('forename')
->required()
->maxLength(255),
Forms\Components\TextInput::make('surname')
->required()
->maxLength(255),
Forms\Components\Select::make('gender')
->options([
'male' => 'Male',
'female' => 'Female',
'other' => 'Other',
'prefer_not_to_say' => 'Prefer not to say'
]),
Forms\Components\DatePicker::make('dob'),
Forms\Components\TextInput::make('contact_number')
->unique(app(Profile::class)->getTable(), 'contact_number', $form->getRecord()->profile ?? null)
->requiredWithout(fn () => dd($form->getComponent('email')->getStatePath()))
->tel()
->maxLength(255)
// ->rules([
// 'contact_number' => 'required_without:email',
// ]),
]),
Fieldset::make('Profile')
->relationship('profile')
->schema([
Forms\Components\TextInput::make('forename')
->required()
->maxLength(255),
Forms\Components\TextInput::make('surname')
->required()
->maxLength(255),
Forms\Components\Select::make('gender')
->options([
'male' => 'Male',
'female' => 'Female',
'other' => 'Other',
'prefer_not_to_say' => 'Prefer not to say'
]),
Forms\Components\DatePicker::make('dob'),
Forms\Components\TextInput::make('contact_number')
->unique(app(Profile::class)->getTable(), 'contact_number', $form->getRecord()->profile ?? null)
->requiredWithout(fn () => dd($form->getComponent('email')->getStatePath()))
->tel()
->maxLength(255)
// ->rules([
// 'contact_number' => 'required_without:email',
// ]),
]),
OH I COULD CRY.... I just needed to pass the absoluteStatePath boolean value london11FACEPALM
Want results from more Discord servers?
Add your server
More Posts