A refused form keeps what was typed
Task: when the server refuses a form, bring it back with what the person typed, not empty.
yaml
# Recipe: a refused form comes back with what was typed, not empty.
paths:
components: ./components
migrations: ./migrations
datasources:
db:
driver: sqlite
database: ./data/messages.dbsql
CREATE TABLE messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL,
body TEXT NOT NULL
);There is nothing to write for it. After a refusal, the next render of a ui:form fills each field with the value sent, once, like the flash. A password or file field never comes back. rows="6" makes the message a multi-line field.
xml
<q:component name="Contact">
<q:action name="send" method="POST">
<q:param name="email" type="email" required="true" />
<q:param name="body" required="true" minlength="20" maxlength="2000" />
<q:query name="saved" datasource="db">
INSERT INTO messages (email, body) VALUES (:email, :body)
<q:param name="email" value="{email}" type="string" />
<q:param name="body" value="{body}" type="string" />
</q:query>
<q:redirect url="/" flash="Thanks, we got your message." />
</q:action>
<ui:window title="Contact us">
<q:if condition="flash">
<ui:alert variant="{flashType == 'error' and 'danger' or 'success'}">{flash}</ui:alert>
</q:if>
<!-- Nothing to write for the values: after a refusal, the next render of
this form fills each field with what was sent (UI-9), once. -->
<ui:form on-submit="send">
<ui:formitem label="Your e-mail"><ui:input bind="email" /></ui:formitem>
<ui:formitem label="Message"><ui:input bind="body" rows="6" /></ui:formitem>
<ui:button variant="primary">Send</ui:button>
</ui:form>
</ui:window>
</q:component>The first test sends a bad address with a good message: the address is refused and the message is still in its field. The second opens the page again: the values are gone.
xml
<q:test name="a refused message comes back as it was typed" page="/">
<test:submit action="send" email="ana@example" body="Your shop is closed on Sundays?" />
<test:expect redirect="/" />
<test:expect error="email" message="Must be a valid email" />
<test:expect text="Your shop is closed on Sundays?" />
<test:expect table="messages" count="0" />
</q:test>
<q:test name="the values come back once: the next visit is empty" page="/">
<test:submit action="send" email="ana@example" body="Your shop is closed on Sundays?" />
<test:visit />
<test:expect no-text="Your shop is closed on Sundays?" />
</q:test>
<q:test name="a valid message is saved" page="/">
<test:submit action="send" email="ana@example.com" body="Your shop is closed on Sundays?" />
<test:expect redirect="/" flash="Thanks, we got your message." />
<test:expect table="messages" count="1" where="email = 'ana@example.com'" />
</q:test>text
tests/contact.test.q
PASS a refused message comes back as it was typed
PASS the values come back once: the next visit is empty
PASS a valid message is saved
3 passed, 0 failed