I am trying to do a simple validation in a livewire component.
I have a “name” field with “required” rule and a “save” button that calls save() function.
When I do this and the input is empty it works great and the error message shows normally:
public function save() {
$this->validate();
}
Then I add a custom validation, and when the input has “asd” in it the error message shows again great.
public function save() {
if ($this->name == "asd") {
$this->addError("name","Name cannot be asd");
}
}
The problem is I cant combine them. If I use them both ($this->validate() and $this->addError()) only the error from the $this->validate() is shown, and when if I put “asd” into the input field no error is shown.
public function save() {
if ($this->name == "asd") {
$this->addError("name","Name cannot be asd");
}
$this->validate();
}
What I expect from the code above is the following:
If input is empty -> show the "name is required" error
If input is "asd" -> show the "name cannot be asd" error
If input is any other thing it passes the validation.
What am i doing wrong?