Hi xxdalexx. Thanks. How would you go about calling your escaping method?
Would you call it manually before validation?
public function create()
{
$this->name = static::string($this->name);
Repo::create(
$this->validate(['name' => 'required|max:32|unique:repos'])
);
session()->flash('message', 'Repository "'.$this->name.'" was created.');
}
This would work but creates a small situation I’d like to avoid…
If you call it but validation fails, $name would be left containing the trimmed version and this would update the version the user can see. If I wanted to avoid this I could capture the original inputs, check them and replace them if a validation exception occurs:
public function create()
{
$name = $this->name;
try {
$this->name = trim($this->name); // whatever cleaning method
Repo::create(
$this->validate(['name' => 'required|max:32|unique:repos'])
);
} catch (\Illuminate\Validation\ValidationException $e) {
$this->name = $name; // replace the original version so what the user sees doesn't change
throw $e;
}
session()->flash('message', 'Repository "'.$this->name.'" was created.');
}
This is rather manual and wordy.
I’m wondering if it’s possible and a good idea for Livewire to change the public properties back to their original values in the case of a ValidationException.
What do you think?