0

I'm using Symfony UX Live Components to edit an NtExam. I get this error when editing the form:

Expected argument of type "string", "null" given at property path "exam_title"

. Code Component:

#[AsLiveComponent]
class ExamEditComponent
{
    #[LiveProp(writable: true)]
    public ?NtExam $ntExam = null;

    public bool $showForm = false;

    public function __construct(private NtExamRepository $ntExamRepository) {}

    public function instantiateForm(): FormInterface
    {
        $exam = $this->ntExam ?? new NtExam();
        return $this->createForm(NtExamType::class, $exam);
    }

    #[LiveAction]
    public function editExam(#[LiveArg] int $id)
    {
        $this->ntExam = $this->ntExamRepository->find($id);
        $this->showForm = true;
    }
}

Edit Button

                <button class="nt-btn-grey" data-action="live#action" data-live-action-param="editExam"
                    data-live-id-param="{{ nt_exam.id }}">{{ ux_icon('lucide:file-pen-line',{height :
                    '16px'}) }}
                    <span>Edit</span></button>

Form Type (NtExamType):

class NtExamType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('exam_title', TextType::class);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults(['data_class' => NtExam::class]);
    }
}

Template: twig

<div>
    {% if component.showForm %}
        {{ form_start(form) }}
        {{ form_row(form.exam_title) }}
        {{ form_end(form) }}
    {% endif %}
</div>

Issue The ntExam object has a valid exam_title (confirmed via dd), but form update send null, causing the error.

1 Answer 1

0

Set the button type to "button", otherwise the UX Live Component will interpret tue button type as a submit, trying to submit the form, so it becomes:

                <button type="button" class="nt-btn-grey" data-action="live#action" data-live-action-param="editExam"
                    data-live-id-param="{{ nt_exam.id }}">{{ ux_icon('lucide:file-pen-line',{height :
                    '16px'}) }}
                    <span>Edit</span></button>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.