Skip to content

Commit

Permalink
Fixes #24583: We should not be able to save a technique with empty en…
Browse files Browse the repository at this point in the history
…um parameters
  • Loading branch information
ElaadF committed Mar 22, 2024
1 parent e9fce38 commit 3746ebb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,34 @@ showTechnique model technique origin ui editInfo =
-- Keep the ID of the method in the UI if it contains invalid parameters value
areErrorOnMethodParameters = List.isEmpty (Dict.keys statesByMethodIdParameter)
areErrorOnMethodCondition = List.isEmpty (Dict.keys statesByMethodIdCondition)
-- Check if enum type is chosen, then we should verify that the list on enum is not empty
isEnumListIsEmpty =
if (List.isEmpty technique.parameters) then
False
else
let
listEnumInputs =
List.map (\parameter ->
case parameter.constraints.select of
Nothing -> False
Just e -> List.isEmpty e
)
technique.parameters
in
List.any identity listEnumInputs
-- Check if enum type is chosen, then verify if there is at least one input (Display name or Value name) who is empty
isEnumWithEmptyInputs =
if (List.isEmpty technique.parameters) then
False
else
let
listEnumInputs =
List.concatMap (\parameter ->
List.concatMap (\opt -> [opt.value, Maybe.withDefault "" opt.name ]) (Maybe.withDefault [] parameter.constraints.select)
)
technique.parameters
in
List.any String.isEmpty listEnumInputs
isUnchanged = case origin of
Edit t -> t == technique
Creation _ -> False
Expand Down Expand Up @@ -299,7 +327,7 @@ showTechnique model technique origin ui editInfo =
text (if (editInfo.open) then "Visual editor " else "YAML editor")
, i [ class "fa fa-pen"] []
]
, btnSave ui.saving (isUnchanged || not (isValid technique ui) || String.isEmpty technique.name || not areErrorOnMethodParameters || not areErrorOnMethodCondition || not areBlockOnError) StartSaving
, btnSave ui.saving (isUnchanged || not (isValid technique ui) || String.isEmpty technique.name || not areErrorOnMethodParameters || not areErrorOnMethodCondition || not areBlockOnError || isEnumListIsEmpty || isEnumWithEmptyInputs) StartSaving
]
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ techniqueParameter model technique param =
TechniqueParameterModified param.id {param | constraints = newConstraint }

constraint = param.constraints
(invalidEnumTypeClass, invalidEnumTypeElem) =
if ("Enum" == selectTypeValue) then
case param.constraints.select of
Nothing ->
("error", ul [class "row"] [ li [ class "text-danger col-md-8" ] [ text "Enum type should contain at least one element" ] ] )
Just opts ->
if(List.isEmpty opts) then
("error", ul [class "row"] [ li [ class "text-danger col-md-8" ] [ text "Enum type should contain at least one element" ] ] )
else
("", text "")
else
("", text "")


in
li [] [
span [ class "border" ] []
Expand Down Expand Up @@ -123,9 +137,9 @@ techniqueParameter model technique param =
, div [ class " form-group" ] [
label [ for typeId] [ text "Type"]
, div [ class "input-group" ] [
select [id typeId, readonly (not model.hasWriteRights), class ("form-select"), value selectTypeValue, onInput onInputType] [
select [id typeId, readonly (not model.hasWriteRights), class ("form-select " ++ invalidEnumTypeClass), value selectTypeValue, onInput onInputType] [
option [ value "Text", selected ("Text" == selectTypeValue) ] [ text "Text"]
, option [ value "Enum", selected ("Enum" == selectTypeValue) ] [ text "Enum"]
, option [ value "Enum", selected ("Enum" == selectTypeValue) ] [ text "Enum"]
]
, label [ class "input-group-text", for checkboxId]
[ input[type_ "checkbox", id checkboxId, checked (not param.mayBeEmpty), onCheck (\c ->
Expand All @@ -141,46 +155,63 @@ techniqueParameter model technique param =
, attribute "data-html" "true"
] [ i [ class "text-info fa fa-question-circle" ] []]
]
, invalidEnumTypeElem
]
]
, ( case constraint.select of
Just l ->
let displayValue = \ index currentValue ->
li[] [
span [ class "border" ] []
, div [ class "param" ] [
div [ class "gm-labels left"] [
div [ class "gm-label rudder-label gm-label-name "] [ text "Enum" ]
]
, div [ class "form-group" ] [
label [] [ text "Display name"]
, input [ readonly (not model.hasWriteRights), type_ "text", class "col-xs-8 form-control", placeholder "Name"
, value (Maybe.withDefault "" currentValue.name), placeholder "Name" , onInput (\s ->
let
newConstraint = { constraint | select = Just (List.Extra.updateAt index (\val -> {val | name = if String.isEmpty s then Nothing else Just s}) l ) }
in
TechniqueParameterModified param.id {param | constraints = newConstraint }), required True] []
]
, div [ class " form-group" ] [
label [ ] [ text "Value"]
, div [class "input-group" ] [
input [ readonly (not model.hasWriteRights), type_ "text", class "form-control", value currentValue.value, placeholder "Value"
, onInput (\s ->
let
displayValue =
\index currentValue ->
let
(invalidEnumNameClass, invalidEnumName) =
if (String.isEmpty (Maybe.withDefault "" currentValue.name)) then
("error", ul [class "row"] [ li [ class "text-danger col-md-8" ] [ text "Enum name cannot be empty" ] ] )
else
("", text "")
(invalidEnumValueClass, invalidEnumValue) =
if (String.isEmpty (currentValue.value)) then
("error", ul [class "row"] [ li [ class "text-danger col-md-8" ] [ text "Enum value cannot be empty" ] ] )
else
("", text "")
in
li[] [
span [ class "border" ] []
, div [ class "param" ] [
div [ class "gm-labels left"] [
div [ class "gm-label rudder-label gm-label-name "] [ text "Enum" ]
]
, div [ class "form-group" ] [
label [] [ text "Display name"]
, input [ readonly (not model.hasWriteRights), type_ "text", class ("col-xs-8 form-control " ++ invalidEnumNameClass), placeholder "Name"
, value (Maybe.withDefault "" currentValue.name), placeholder "Name" , onInput (\s ->
let
newConstraint = { constraint | select = Just (List.Extra.updateAt index (\val -> {val | value = s}) l ) }
newConstraint = { constraint | select = Just (List.Extra.updateAt index (\val -> {val | name = if String.isEmpty s then Nothing else Just s}) l ) }
in
TechniqueParameterModified param.id {param | constraints = newConstraint }), required True] []
, invalidEnumName
]
, div [ class " form-group" ] [
label [ ] [ text "Value"]
, div [class "input-group" ] [
input [ readonly (not model.hasWriteRights), type_ "text", class ("col-xs-8 form-control " ++ invalidEnumValueClass), value currentValue.value, placeholder "Value"
, onInput (\s ->
let
newConstraint = { constraint | select = Just (List.Extra.updateAt index (\val -> {val | value = s}) l ) }
in
TechniqueParameterModified param.id {param | constraints = newConstraint }), required True] []
, invalidEnumValue
]
]
]
, div [ class "remove-item", onClick(
let
newConstraint = { constraint | select = Just (List.Extra.removeAt index l ) }
in
TechniqueParameterModified param.id {param | constraints = newConstraint }) ] [
i [ class "fa fa-times"] []
]
]
, div [ class "remove-item", onClick(
let
newConstraint = { constraint | select = Just (List.Extra.removeAt index l ) }
in
TechniqueParameterModified param.id {param | constraints = newConstraint }) ] [
i [ class "fa fa-times"] []
]
]
in
ul [ class "files-list parameters"]
( List.concat [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1695,9 +1695,12 @@ ul li.hide-method {
}
.error{
animation: shake 0.2s ease-in-out 0s 2;
box-shadow: 0 0 0.5em;
border-color: #DA291C !important;
}
.form-select:focus.error{
outline: 0;
box-shadow: 0 0 0 0.25rem #DA291C30;
}
@keyframes shake {
0% { margin-left: 0rem; }
25% { margin-left: 0.5rem; }
Expand Down

0 comments on commit 3746ebb

Please sign in to comment.