valentin_vranic's avatar

Fill property before validation? Livewire Form

Is there a possibility to fill a public property before validation in Livewire Form? Like prepareForValidation in FormRequest?

Actually what I want to achieve is, to make description required, but there are two occasions. One is when it appears in the form, and one when not. Now, for the second scenario I want to fill it up, to pass the required rule.

And I want to fill it up based on another input field.

Something like this: $this->description = $this->description ?? CrmOperators::where('crm_id', $this->crmId)->first('crm_name'); This is supposed to handle the criteria for both scenarios.

The crmId comes from form also.

Is it solvable, or I should find another way to achieve it?

0 likes
6 replies
LaryAI's avatar
Level 58

Yes, you can achieve this in Livewire by hooking into the component's lifecycle methods to prepare your data before validation. Livewire offers a updating lifecycle hook that you can use to set your description property based on another input field before the validation occurs.

Here's how you can do it:

use Livewire\Component;

class YourComponent extends Component
{
    public $description;
    public $crmId;

    public function updating($propertyName)
    {
        if ($propertyName === 'crmId') {
            $this->fillDescription();
        }
    }

    public function fillDescription()
    {
        if (empty($this->description)) {
            $crmOperator = CrmOperators::where('crm_id', $this->crmId)->first();
            $this->description = $crmOperator ? $crmOperator->crm_name : '';
        }
    }

    public function submit()
    {
        $this->validate([
            'description' => 'required',
            // other validation rules...
        ]);

        // Handle the form submission...
    }

    public function render()
    {
        return view('livewire.your-component');
    }
}

In this example, the updating method is called every time a property is updated. When the crmId property is updated, it calls the fillDescription method to set the description property if it's not already set. This way, when you call the submit method, the description property will be filled and will pass the required validation rule.

Remember to replace YourComponent with the actual name of your Livewire component and adjust the render method to return the correct view for your component.

valentin_vranic's avatar

@LaryAI I'm extending the Livewire Form, which one is initialized inside a Modal (which extends Component). Because the Form does not fire the Livewire lifecycle hook updated, I'm trying to make it work inside the Modal...

valentin_vranic's avatar
Level 5

@LaryAI I've figured out that there is accessible prepareForValidation for Livewire\Form too.

protected function prepareForValidation($attributes): array

valentin_vranic's avatar

@arifhossen Thanks. I'm using it for some cases, but currently I want it to be required, just in one case when it's not in the form, to fill it up before validation

Please or to participate in this conversation.