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

Cannot add items to nullable array #3102

Open
cromoteca opened this issue Dec 31, 2024 · 1 comment · May be fixed by #3116
Open

Cannot add items to nullable array #3102

cromoteca opened this issue Dec 31, 2024 · 1 comment · May be fixed by #3116
Assignees
Labels
bug Something isn't working hilla Issues related to Hilla

Comments

@cromoteca
Copy link
Contributor

cromoteca commented Dec 31, 2024

Describe the bug

This issue comes from https://vaadin.com/forum/t/nested-form-issue-typeerror-cannot-read-properties-of-undefined-reading-0/168014. The post contents are copied below.

I trying to use it for simple invoicing application (one invoice can have multiple invoice rows)

When I click on the “Add button”, then I get following error: TypeError: Cannot read properties of undefined (reading '0') at _BinderNode.initializeValue (.

Expected-behavior

No response

Reproduction

The react component looks like this:

function InvoiceItemForm({model, remove}: { model: InvoiceItemModel, remove: () => void }) {
    const {field} = useFormPart(model);

    return (
        <div>
            <NumberField {...field(model.quantity)} />
            <Button onClick={remove}>Remove</Button>
        </div>
    );
}

export default function GroupFormView() {
    const { field, model } = useForm(TransactionModel);
    const { items, value, setValue } = useFormArrayPart(model.items);

    return (
        <>
            <TextField {...field(model.description)} />
            {items?.map((item, index) => (
                <InvoiceItemForm key={index} model={item} remove={() => setValue(value!.filter((_, i) => i !== index))}/>
            ))}
            <Button onClick={() => setValue([...(value ?? []), InvoiceItemModel.createEmptyValue()])}>Add item</Button>
        </>
    );
}

The java entity looks like this

@Entity
@Table(name = "transactions")
...
public class Transaction {

...

  @OneToMany(mappedBy = "transaction", cascade = CascadeType.ALL)
  private List<InvoiceItem> items = new ArrayList<>();

}

@Entity
@Table(name = "invoice_items")
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Getter
@Setter
public class InvoiceItem {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long itemId;

  @ManyToOne
  @JoinColumn(name = "transaction_id", nullable = false)
  private Transaction transaction;

  @NotNull
  @Min(1)
  private Integer quantity;

}

System Info

I use Vaadin version 24.6.0. Java 23, Lombok.

@cromoteca cromoteca added bug Something isn't working hilla Issues related to Hilla labels Dec 31, 2024
@KardonskyRoman
Copy link

KardonskyRoman commented Jan 1, 2025

👍 I have been using workaround for this behavier more than 2 years in different projects and thought this is done on purpose. Great to see it will be fixed

@cromoteca cromoteca self-assigned this Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hilla Issues related to Hilla
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants