Validate length of variant fields

This commit is contained in:
David Cook
2023-09-20 14:59:54 +10:00
committed by Rachel Arnould
parent 875d083a1d
commit 9a9be8dacd
3 changed files with 19 additions and 4 deletions

View File

@@ -61,6 +61,7 @@ module Spree
localize_number :price, :weight
validates_lengths_from_database
validate :check_currency
validates :price, numericality: { greater_than_or_equal_to: 0 }, presence: true
validates :tax_category, presence: true,

View File

@@ -67,15 +67,18 @@
- product.variants.each do |variant|
= form.fields_for("products][][variants_attributes][", variant, index: nil) do |variant_form|
%tr.condensed
%td.align-left
%td.field
= variant_form.hidden_field :id
= variant_form.text_field :display_name, 'aria-label': t('admin.products_page.columns.name'), placeholder: product.name
%td.align-right
= error_message_on variant, :display_name
%td.field
= variant_form.text_field :sku, 'aria-label': t('admin.products_page.columns.sku')
= error_message_on variant, :sku
%td.align-right
.content= variant.unit_to_display
%td.align-right
%td.field
= variant_form.text_field :price, 'aria-label': t('admin.products_page.columns.price'), value: number_to_currency(variant.price, unit: '')&.strip # TODO: add a spec to prove that this formatting is necessary. If so, it should be in a shared form helper for currency inputs
= error_message_on variant, :price
%td.align-right
.content= variant.on_hand || 0 #TODO: spec for this according to requirements.
%td.align-left

View File

@@ -263,11 +263,15 @@ describe 'As an admin, I can see the new product page' do
end
end
it "shows errors for product fields" do
it "shows errors for both product and variant fields" do
within row_containing_name("Apples") do
fill_in "Name", with: ""
fill_in "SKU", with: "A" * 256
end
within row_containing_name("Medium box") do
fill_in "Name", with: "L" * 256
fill_in "SKU", with: "1" * 256
end
expect {
click_button "Save changes"
@@ -282,6 +286,13 @@ describe 'As an admin, I can see the new product page' do
expect(page).to have_content "is too long"
end
pending
within row_containing_name("L" * 256) do
expect(page).to have_field "Name", with: "L" * 256
expect(page).to have_field "SKU", with: "1" * 256
expect(page).to have_content "is too long"
expect(page).to have_field "Price", with: "10.25"
end
expect(page).to have_content "Please review the errors and try again"
end
end