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

array.contains doesn't work with arrays of enums #425

Closed
cspital opened this issue Apr 1, 2022 · 0 comments · Fixed by #426
Closed

array.contains doesn't work with arrays of enums #425

cspital opened this issue Apr 1, 2022 · 0 comments · Fixed by #426

Comments

@cspital
Copy link
Contributor

cspital commented Apr 1, 2022

Given an object like:

public class WeatherForecastSearchRequest
{
    [FromQuery(Name = "strings")]
    public List<string> Strings { get; set; } = new List<string>();

    [FromQuery(Name = "since")]
    public DateTime? Since { get; set; }

    [FromQuery(Name = "types")]
    public List<WeatherType> Types { get; set; } = new List<WeatherType>();

    [FromQuery(Name = "verbose")]
    public bool Verbose { get; set; }
}

public enum WeatherType
{
    Guess,
    Known
}

And a template like:

<div class='search'>
    <form name='Search' method='get' action>
        <div class='property'>
            <label class='name' for='strings'>Strings:</label>
            <input id='strings' type='text' name='strings' value='{{ Form.Strings | array.join ',' }}' />
        </div>
        <div class='property'>
            <label class='name' for='since'>Since:</label>
            <input id='since' type='datetime-local' name='since' value='{{ Form.Since | date.to_string '%Y-%m-%dT%H:%M:%S' }}' />
        </div>
        <div class='property'>
            <label class='name' for='types'>Types:</label>
            <select name='types' id='types' multiple>
                <option value='Guess' {{ selected = ''; if Form.Types | array.contains 'Guess'; selected = 'selected'; end; selected }}>Guess</option> // this line doesn't work as is
                <option value='Known' {{ selected = ''; if Form.Types | array.contains 'Known'; selected = 'selected'; end; selected }}>Known</option> // this line doesn't work as is
            </select>
        </div>
        <div class='property'>
            <label class='name' for='verbose'>Verbose:</label>
            <input id='verbose' type='checkbox' name='verbose' value='true' {{ checked = ''; if Form.Verbose; checked = 'checked'; end; checked }}/>
        </div>
        <input type='submit' value='Search'>
    </form>
</div>

Lines like the following do not work as expected.

<option value='Guess' {{ selected = ''; if Form.Types | array.contains 'Guess'; selected = 'selected'; end; selected }}>Guess</option>

Instead we have to do the following to marshall the array to an array of strings.

<option value='Guess' {{ selected = ''; if Form.Types | array.each @string.strip | array.contains 'Guess'; selected = 'selected'; end; selected }}>Guess</option>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants