Form to check if a query returns a record

Hi,

I have a form and user enters their code and I am trying to connect it to a Server action which runs a query based on the code they entered, if the query returns a result (I used ‘count’ here, there will always be 1 or nil counts), then they can be redirect to a specific page, otherwise an error message shoulder appear.

I have tried several ways without much success. It’s not the same as check if record exists in a database (https://docs.wappler.io/t/check-if-a-database-record-already-exists-before-inserting-a-new-entry/2847)!

Here is what I have so far… also the redirect doesn’t work.
Any suggestions would be helpful. Thank you.

Here is my server actions:

You cannot use serverside redirect for that. You need to use the browser component on the page and use it to redirect.
You can achieve this using a response step in the server action:

Condition - query count != 1

  • Then -> response 400 / invalid
  • Else -> empty

On the front end, on form dynamic events:

  • server action onsuccess -> browser goto … some page
  • serveraction oninvalid -> show some alert/notification/toast

Hi @Teodor, I am trying this now. For “response” on Server action, what do I put in “Name”, “Status” and “Text” for the “then” step?

Name - whatever you like
Status - 400
Text - "some string" .

@Teodor, half way there. alert comes up, but when correct the browser goto doesn’t work and still displays alerts.

Not sure what I am doing wrong.
Just confirming, the “then” “text” is “some string” - does it have to be anything specific.
I have checked the query to make sure it works and returns a value.

It can be any string, literally. The status code 400 is the important one.

So what do you see in your browser dev tools when you expect the condition to be false (i.e. query count == 1)?

How did you configure the form events?

@Teodor, In the browser dev tool, it gives me a “400” response when the condition is false (query count ==1).
On checking the query again, it seems to be giving me "some string’ output regardless.

Form sure/App if have



If you get a response of 400, then then the condition is met … i.e. count does not equal 1. Maybe check how do you filter the query and check if the record really exists …

It look like the ‘count’ in the query isn’t working, even though the query does return a record. I tried it without the count and just put a value that exists in the table and it worked. So not sure why count isn’t working?

I don’t know why is it not working in your case, i just tested this and it works fine.
What query are you using - single, paged, multiple?
What server model are you using?

Strange isn’t it. Its a single query, PHP model. I have manually put in values in the query that return a results for testing, but still not working.

Well ... single queries do not return array. You can't use the count formatter with it.
For single query just use {{!query}} as a condition :slight_smile: where query is the name of your query of course.

Perfect @Teodor. It worked. Once again, you are genius! :slight_smile: Thank you. I place {{!query}} and all worked fine.

1 Like

@Teodor, just a quick one, I am trying to limit/stop the user from entering more than 4 digits in an input form field - lets the user enter more than four. anyway we can restrict so they.
Maxlength doesn’t seem to stop the user entering more than 4.
What am I doing wrong?

< input type="number" class="form-control style2 form-control-lg" id="inputVerifyCode" name="inputVerifyCode" aria-describedby="input1_help" placeholder="4-Digit Code" required="" data-rule-digits="" data-msg-pattern="" minlength="4" maxlength="4" data-msg-min="" data-msg-max="" data-msg-minlength="Enter your 4 digit code." data-msg-maxlength="Enter your 4 digit code." data-msg-required="Enter your 4 digit code.">

Look for warnings in the dev console. Perhaps the validation scripts are not added or you haven’t saved your layout, which is where the scripts would be.

1 Like

The maxlength attribute is not available for inputs having type="number", it’s available for text, password etc.

1 Like