Custom Validation
I'm having a 2 fields: (1) Price (2) Checkbox
Task 1) If the price is less than a specific amount then the checkbox check is required.
Task 2) Display a validation error message only if the price is less than a specific amount. Form submit will be allowed
I've done below code and it's not working properly:
Forms\Components\TextInput::make('price')
->label('Price'),
Forms\Components\Toggle::make('checkbox')
->label('Checkbox')
->rules(['required_if:price,(X Amount)']),
I've tried with custom rule but it's not working properly.
How can I do this?
28 Replies
1) Set the field required if price is less than 100
2) Set the validation attribute
Forms\Components\TextInput::make('price')
->reactive()
->label('Price'),
Forms\Components\Toggle::make('checkbox')
->label('Checkbox')
->reactive()
->required(fn(Closure $get, $set) => (float) $get('price') < 100)
->validationAttribute('You must check if value less than 100'),
Ummm I wouldn't add the validation attribute there,
- it's not needed
- it would mess up the validation message (it is validation attribute not validation message)
Cool, add your own advice then 🙂
Thank you @toeknee_iom
It's not displaying validation message.
You could use a custom validation rule, but I was trying to keep it simple for you.
Can we do it with custom validation?
Right now I'm getting this only as a response. No validation error messages on the form
Forms\Components\TextInput::make('price')
->label('Price'),
Forms\Components\Toggle::make('checkbox')
->label('Checkbox')
->rules([
function (Closure $get, $state) {
\Log::info("Price: " . $get('profile_data_combined_household_income'));
\Log::info("Actual: " . (100));
if ($get('profile_data_combined_household_income') < (100)) {
\Log::info("In ");
return function (string $attribute, $value, Closure $fail) {
$fail("You must check if value less than " . (100));
};
}
},
]),
This works and gets the record on the Log file, but from submit process is continue going. Not get any error message or etc.
You will need to cast your get to an integer.
Does Log::info 'In' get hit?
Yes, I got it
It looks ok to me. Can't see why it wouldn't be responding
Maybe use
rule()
(not plural) and without the array. Not sure if we support closures for every array item.
Also you could try to use Validator Facade in afterValidation hook on that page to do a custom validationIt's not working.
Should I try with a livewire validation process like this?
$this->addError('email', 'The email field is invalid.');
What is not working?
plural: rule()
Validator Facade in afterValidation hook
both not working.
Show some code you used. And please format it as stated in #✅┊rules
Right now I've done like this:
Another Custom Rule:
So you didn't try?
Yes, I tried it not working.
I guess you need a DataAware rule then
Instead of
$get
.What about afterValidate()?
https://filamentphp.com/docs/2.x/admin/resources/editing-records#lifecycle-hooks
Filament
Editing records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
That what I recommended, too. But only got "not working" as an answer
@Kuldeep ?
Let me try once
If it doesn't work, show your code
You can use $this->data to get the values
update error messages if you want:
After submit form it's going directly to the the method: completeProfile()
You can develop the validations in completeProfile method
My suggestion is about resources
afterValidate
is an admin-panel thing
Just use a Validator
in completeProfile
thenOk, @Dennis Koch @Leandro Ferreira @toeknee_iom Thank you for your support