Todo tipo de campo
Tradução automática
Esta página foi traduzida automaticamente do inglês e ainda não foi revisada por um falante nativo; correções são bem-vindas no GitHub. Se algo não bater, vale o original em inglês. O código e os resultados são os mesmos do original, importados dos arquivos testados.
Tarefa: um formulário que usa cada tipo de campo, com rótulos.
yaml
# Recipe: every kind of field, laid out with ui:form and ui:formitem.
paths:
components: ./componentsui:formitem label="…" coloca um rótulo ao lado do seu campo. ui:select pega as escolhas de ui:option com os seus próprios textos, e ui:radio as pega do enum do q:param da ação. Um ui:switch é uma caixa booleana, e rows faz um campo de várias linhas. As regras de cada campo vêm da ação (Cada erro no seu campo).
xml
<q:component name="Booking">
<q:action name="book" method="POST">
<q:param name="name" required="true" minlength="2" />
<q:param name="nights" type="integer" required="true" min="1" max="14" />
<q:param name="room" enum="single,double,suite" default="double" />
<q:param name="breakfast" type="boolean" default="false" />
<q:param name="arrival" enum="morning,afternoon,night" default="afternoon" />
<q:param name="notes" maxlength="300" default="" />
<q:redirect url="/" flash="{name}: {nights} nights, {room}, breakfast {'yes' if breakfast else 'no'}, {arrival}." />
</q:action>
<ui:window title="Book a room">
<ui:vbox gap="md" padding="lg" width="480">
<q:if condition="flash">
<ui:alert variant="{flashType == 'error' and 'danger' or 'success'}">{flash}</ui:alert>
</q:if>
<!-- ui:formitem puts a label next to its field. The fields take their
rules (and the lists of choices) from the action's q:params. -->
<ui:form on-submit="book" submit="Book">
<ui:formitem label="Name"><ui:input bind="name" /></ui:formitem>
<ui:formitem label="Nights"><ui:input bind="nights" /></ui:formitem>
<ui:formitem label="Room">
<ui:select bind="room">
<ui:option value="single">Single</ui:option>
<ui:option value="double">Double</ui:option>
<ui:option value="suite">Suite</ui:option>
</ui:select>
</ui:formitem>
<ui:switch bind="breakfast" label="Breakfast" />
<ui:formitem label="Arrival"><ui:radio bind="arrival" /></ui:formitem>
<ui:formitem label="Notes"><ui:input bind="notes" rows="3" /></ui:formitem>
</ui:form>
</ui:vbox>
</ui:window>
</q:component>xml
<q:test name="the form shows its labels, the option texts and the button" page="/">
<test:visit />
<test:expect text="Name" />
<test:expect text="Single Double Suite" />
<test:expect text="Breakfast" />
<test:expect text="morning" />
<test:expect text="Book" />
</q:test>
<q:test name="the fields reach the action typed" page="/">
<test:submit action="book" name="Ana" nights="3" room="suite" breakfast="on" arrival="night" />
<test:expect redirect="/" flash="Ana: 3 nights, suite, breakfast yes, night." />
</q:test>
<q:test name="left out, the defaults apply" page="/">
<test:submit action="book" name="Ana" nights="1" />
<test:expect flash="Ana: 1 nights, double, breakfast no, afternoon." />
</q:test>
<q:test name="a refused field keeps the others as typed" page="/">
<test:submit action="book" name="Ana" nights="20" notes="Late check-in, please." />
<test:expect error="nights" message="Must be at most 14 (got 20)" />
<test:expect text="Late check-in, please." />
</q:test>text
tests/booking.test.q
PASS the form shows its labels, the option texts and the button
PASS the fields reach the action typed
PASS left out, the defaults apply
PASS a refused field keeps the others as typed
4 passed, 0 failed