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

Compare input type="number" values using if? #193

Closed
jgollerilumy opened this issue Oct 22, 2019 · 9 comments
Closed

Compare input type="number" values using if? #193

jgollerilumy opened this issue Oct 22, 2019 · 9 comments
Labels

Comments

@jgollerilumy
Copy link

Hello everyone,

First of all thanks for the great extension to everyone involved. Have been using it for a while now and it works great!

I've been trying to change the Text on a email notification based on the numeric value of an input.

Basically, if the user enters a value smaller than 10000, show "Text X" else show "Text Y"

Is that possible?

I was hesitant to post here, because Contao seems to be missing the option to select an input type 'number' in the form generator (that im pretty sure used to exist at some point in the past). So the Issue might be there.

That's why I've tried to work around it by creating a custom html-field of the _input type="number" name="test", but it seems the notification_center does not recognize that. ##form_test## is not replaced with its value in the Email.

I've also posted my Question to the German contao Forums

I'm aware this is not a support service, but im thankful for any hints if and how this could be realized even if some coding would be required.

@Toflar
Copy link
Member

Toflar commented Oct 22, 2019

Is that possible?

Yes. You can use if-else statements with Contao's simple tokens. You may want to check the docs: https://docs.contao.org/manual/de/de/core-erweiterung/newsletter/newsletter-verwaltung/

You can also see that it works because it's all covered by unit tests in the Contao core: https://github.com/contao/contao/blob/master/core-bundle/tests/Contao/StringUtilTest.php#L122-L180

I was hesitant to post here, because Contao seems to be missing the option to select an input type 'number' in the form generator (that im pretty sure used to exist at some point in the past).

That's not true, just select the text type and make sure the validation is set to Numeric characters.

@asaage
Copy link

asaage commented Oct 22, 2019

You get a input type="number" if you validate your Textfield for "numeric characters".

Not entirely shure about the possibilities of logic operators though.
I think this might be a usecase for https://github.com/terminal42/contao-conditionalformfields where you put TextX and Y in Hidden fields.

@jgollerilumy
Copy link
Author

Impressive response time. Thanks!

That's not true, just select the text type and make sure the validation is set to Numeric characters.

This is exactly what I tried first, when doing so and using this:

{if form_feld<10000} Text X {else} Text Y {endif}

it always triggered the {else}

statements with the == operator always worked with no problems for me.

But now im hopeful i just made a mistake so I will try again! will update soon.

@asaage
Copy link

asaage commented Oct 22, 2019

it's probably
{if feld<10000} not {if form_feld<10000}

@jgollerilumy
Copy link
Author

jgollerilumy commented Oct 22, 2019

@asaage

it's probably
{if feld<10000} not {if form_feld<10000}

Thanks for the hint, but {if feld<10000} is not a valid token (i can't save the notification if i put it like that)

I've tried again now and it's true that the text field with "numeric characters" creates a type="number" (not sure why i assumed it doesnt)

After reading @Toflar first link, i can confirm that i can use any of the statements mentioned in the article with no problems. They all use the == operator though.

After Reading into Toflar's second link found the tests for the Operator I want to use.

        yield 'Test comparisons (<) with regular characters (match)' => [
            'This is my {if value<0}match{endif}',
            ['value' => -5],
            'This is my match',
        ];
        yield 'Test comparisons (<) with regular characters (no match)' => [
            'This is my {if value<0}hello{endif}',
            ['value' => 9],
            'This is my ',
        ];

So, do i have to change something else in my condition in order to compare values with '<' other than changing the operator itself?

{if form_feld==10000} Text X {else} Text Y {endif}

works just fine, while this:

{if form_feld<10000} Text X {else} Text Y {endif}

doesnt

@Toflar
Copy link
Member

Toflar commented Oct 22, 2019

I just tried the unit tests again and it works for me so I'm sorry but there's nothing else we can do here for you. Just make sure you use the latest versions of Contao (4.4.44 or 4.8.4) and the NC (1.5.8).

@jgollerilumy
Copy link
Author

Okay. Thanks!

I redid it on a different system that runs 4.4.44 / 1.5.8.

Unfortunately the problem still persists. I even tried with negative values and comparing to 0 like in the unit test but to no success. I'm all out of ideas now.

I will try to find someone else to confirm/deny this and let you know either way.

@jgollerilumy
Copy link
Author

I'm scratching my head here. Unfortunately i also couldnt find anyone on the contao forums yet to either confirm or deny the problem im having.

In the meantime, @Toflar can you confirm that this should be the correct statement in a notification?

{if form_feld<10000} Text X {else} Text Y {endif}

@Toflar
Copy link
Member

Toflar commented Oct 31, 2019

Jup, looks okay to me.

@Toflar Toflar closed this as completed Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants