F
Filament9mo ago
SirFat

validateOnly checks the rules, but requires a 'Submit' action to show the outcome.

Hiya! I'm trying to leverage validateOnly after a custom action which checks the validity of some data. I'm able to use ->rules([]) to define what these rules are, but I'm forced to hit 'submit' in order to have the validation messages populate. Is there another way to force the validation message to show up on demand?
Solution:
No, I followed that process. That works in the case of the field validation itself. But if the field does not change, then there is no reason for the TextInput afterStateUpdated hook to be called. Instead, as it's calling an Action, if you want to force validation and have it behave properly, it would appear you need to call callAfterStateUpdated() on the Component from within the after() method on the Action. ```php...
Jump to solution
6 Replies
krekas
krekas9mo ago
Does your input have live()?
SirFat
SirFat9mo ago
Yep I’ll double check via debugging that after state updated is being called, because technically, as the value in the field isn’t changing (the action takes the information and validates it based on the external APi) maybe it isn’t firing the validateOnly - although I was pretty sure I checked that. What would be the best case: if failed verification, validation error invoked and visible straight away. If successful, it’s marked as such with a visual representation that the data was good (I wonder if a suffix icon could be used and switched? How do you interact with an input other than just setting its value via Set? Is that even possible?
SirFat
SirFat9mo ago
I've at least forced it to run the validate message by calling $component->callAfterStateUpdated() from within the action, which fires off the validation - but the rule breakage is reflected as a thrown exception rather than being presented in the validation messages (per the screenshot).
No description
SirFat
SirFat9mo ago
I think the issue may actually be that the afterStateUpdated hook is occurring before the action returns and i'm able to set the appropriate data, so for that period, the data is valid. (which is why when calling callAfterStateUpdated() manually it does produce the right error, but it's not caught by the wrapper in order to present it as a validation message, rather a thrown exception.
Solution
SirFat
SirFat9mo ago
No, I followed that process. That works in the case of the field validation itself. But if the field does not change, then there is no reason for the TextInput afterStateUpdated hook to be called. Instead, as it's calling an Action, if you want to force validation and have it behave properly, it would appear you need to call callAfterStateUpdated() on the Component from within the after() method on the Action.
->suffixAction(
Action::make('validateImei')
->icon('heroicon-o-magnifying-glass')
->before(
// reset fields that we are intending to process.
function (Get $get, Set $set, HasForms $livewire, TextInput $component): void {
$set("imei_type", "");
$set("imei_error", "");
}
)
->after(
// run our validation now we have cleaned our fields. .
function (Get $get, HasForms $livewire, TextInput $component): void {
$component->callAfterStateUpdated();
}
)
->action(function (Get $get, Set $set, TextInput $component, Component $livewire, $state) {
// ....
})
// ....
),
->suffixAction(
Action::make('validateImei')
->icon('heroicon-o-magnifying-glass')
->before(
// reset fields that we are intending to process.
function (Get $get, Set $set, HasForms $livewire, TextInput $component): void {
$set("imei_type", "");
$set("imei_error", "");
}
)
->after(
// run our validation now we have cleaned our fields. .
function (Get $get, HasForms $livewire, TextInput $component): void {
$component->callAfterStateUpdated();
}
)
->action(function (Get $get, Set $set, TextInput $component, Component $livewire, $state) {
// ....
})
// ....
),