Validation exception of subcomponent bubble up to parent component

I have a product component with fields: title and description.
Inside there there is also a FileUpload subcomponent.

Considering that the FileUpload has a $files variable that is required
when the user tries to create a new product without files I want the
file validation exception to bubble up to the product component so to cancel the creation of the product.

How can I do this?

class Product extends Component
{
   public $title;
   public $description;

	public function rules()
   {
	    return [
           'title' => 'required',
           'description' => 'required',
        ];
   }
}
class FileUpload extends Component
{
   public $files = [];

	public function rules()
   {
       return [
           'files' => 'required',
       ];
   }
}

considering the relationship between product and images (eg. one-many)

public $product = [];
public $images = [];

public function mount()
{
    $this->product = Product::all();
    $this->images = $this->product->images;  //or the subcomponent images data retrieved
}

public function rules()
{
    return [
        'product.title' => 'required'|'min:2',
        'product.description' => 'required'|'min:10',
        'images.*' => 'required'|'image'
    ];
}

Thanks for the answer but this is not what I want. I edit my question to be less confusing. Imagine that I have two separate and independent components - products and upload files.

I’d create a trait to handle the file upload (assuming you’re using it in more than one place) and bring all of your Product logic into one component.

1 Like