have you tried throwing a validation error instead?
Livewire - direct error message - not working
I need to add a validation error in a complex situation.
To do so, I thought I will have my own condition to check if there's an error and add the error to the errors manually.
Following the Livewire documentation, I have no success.
From the documentation:
"You can also add custom key/message pairs to the error bag.
$this->addError('key', 'message')"
And this:
"The validate() and validateOnly() methods should handle most cases, but sometimes you may want direct control over Livewire's internal ErrorBag.
Livewire provides a handful of methods for you to directly manipulate the ErrorBag.
From anywhere inside a Livewire component class, you can call the following methods:
// Quickly add a validation message to the error bag. $this->addError('email', 'The email field is invalid.');"
In my component:
protected $rules =
[
'roastReportName' => 'required',
'greenBeanId' => 'required',
'roastTypeId' => 'required',
'stickerId' => 'required',
'weights.*.price' => 'numeric'
];
public function storeBean()
{
$this->validate();
$this->addError('stickerId', 'This is a test.');
...
}
In my blade:
@if($errors->any())
<div class="alert alert-danger">
<p><strong>Opps Something went wrong</strong></p>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
I submit the form with errors, all the errors display besides the one I added manually.
From the documentation:
"// This will give you full access to the error bag.
$errors = $this->getErrorBag();
// With this error bag instance, you can do things like this:
$errors->add('some-key', 'Some message');"
I tried:
public function storeBean()
{
$this->validate();
// This will give you full access to the error bag.
$errors = $this->getErrorBag();
// With this error bag instance, you can do things like this:
$errors->add('stickerId', 'Some message');
Error does not display
From the documentation:
Sometimes you may want to access the Validator instance that Livewire uses in the validate() and validateOnly() methods. This is possible using the withValidator method. The closure you provide receives the fully constructed validator as an argument, allowing you to call any of its methods before the validation rules are actually evaluated.
use Illuminate\Validation\Validator;
class ContactForm extends Component
{
public function save()
{
$this->withValidator(function (Validator $validator) {
$validator->after(function ($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
});
})->validate();
}
}
I tried:
public function storeBean()
{
$this->validate();
$this->withValidator(function (Validator $validator) {
$validator->after(function ($validator) {
//no condition here, just want to test if the error is being added
$validator->errors()->add('stickerId', 'Something is wrong with this field!');
});
})->validate();
...}
Nope.
What am I missing?
@SigalZ All right, that makes sense. That should be fairly easy to do with the standard rules, without having to access the validator or anything like that. Essentially, I think you just need a required_with rule for your bag dropdown, dependent on the price field:
protected $rules =
[
'roastReportName' => 'required',
'greenBeanId' => 'required',
'roastTypeId' => 'required',
'stickerId' => 'required',
'weights.*.price' => 'numeric',
'weights.*.selectedBag' => 'required_with:weights.*.price'
];
// Add a message to appear if price is given but not bag
protected $messages = [
'weights.*.selectedBag.required_with' = "You must select a bag if you provide a price"
];
public function storeBean()
{
$this->validate();
...
}
}
Please or to participate in this conversation.