Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change another value of an entity using a LiveComponent #1982

Closed
Matoto951 opened this issue Jul 12, 2024 · 4 comments
Closed

Change another value of an entity using a LiveComponent #1982

Matoto951 opened this issue Jul 12, 2024 · 4 comments

Comments

@Matoto951
Copy link

I have a Quote Entity with relations (QuoteVersion that also has sub-relations).
for context (Project -> Quote -> QuoteVersion -> Chunk -> ChunkDetail)

The live component I created for this form looks like:

#[LiveProp(onUpdated: 'updateField')]
public Quote $initialFormData;

#[LiveProp]
public Project $project;

#[LiveProp]
public ?QuoteVersion $quoteVersion = null;

protected function instantiateForm() : FormInterface
{
    $quote = $this->initialFormData;
    $quote->setProject($this->project);

    $quoteVersion = $this->quoteVersion ?? (new QuoteVersion())->setLanguage(Language::LANGUAGE_EN)->addChunk((new Chunk()));
    $quote->addQuoteVersion($quoteVersion);

    return $this->createForm(QuoteType::class, $quote, [
        'project' => $this->project,
        'quoteVersion' => $quoteVersion
    ]);
}

public function updateField(): void
{
	//$this->formValues['legalNotes'] = 'helloooo';
	dd($this->formValues);
}

My goal is to set a field in the updateField method whenever my form is re-rendered for validation, but my dd function never triggers.

onUpdated seems to be working only for props that are not Entities. Is there any way around this or am I just doing it wrong?

@smnandre
Copy link
Member

You must use writable to define what is writable in your LiveProp... or i suppose it won't be updated.

Maybe then try with

#[LiveProp(writable: ['title', 'content'], onUpdated: ['title' => 'onTitleUpdated'])]

https://symfony.com/bundles/ux-live-component/current/index.html#writable-object-properties-or-array-keys

@GonZOO82
Copy link

onUpdated don't working on object :/

@smnandre
Copy link
Member

The example in the documentation does not work for you ?

https://symfony.com/bundles/ux-live-component/current/index.html#add-a-hook-on-liveprop-update

@GonZOO82
Copy link

If I use the writable: true, does not work onUpdated on the property.

#[LiveProp(writable: true, onUpdated: ['title' => 'onTitleUpdated'])]
public Post $post;

I just read this

By default, the user can't change the properties of an entity LiveProp You can allow this by setting writable to property names that should be writable. This also works as a way to make only some keys of an array writable:

Question:
How can i catch the value of property when the form changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants