|
| 1 | +<script lang="ts"> |
| 2 | + import { page } from '$app/stores'; |
| 3 | + import { superForm } from '$lib/client'; |
| 4 | + import SuperDebug from '$lib/client/SuperDebug.svelte'; |
| 5 | +
|
| 6 | + export let data; |
| 7 | +
|
| 8 | + const { form, errors, message, enhance } = superForm(data.form, { |
| 9 | + dataType: 'json', |
| 10 | + taintedMessage: null, |
| 11 | + validators: { |
| 12 | + business_id: (input) => |
| 13 | + input ? undefined : 'Please fill business name first' |
| 14 | + /* |
| 15 | + // Uncomment for testing purposes, but will fail browser test |
| 16 | + shareholders: { |
| 17 | + id_issuance_date: (date) => (date ? 'Nope' : null) |
| 18 | + } |
| 19 | + */ |
| 20 | + }, |
| 21 | + onError({ result, message }) { |
| 22 | + console.log('error'); |
| 23 | + console.log(result); |
| 24 | + console.log(message); |
| 25 | + } |
| 26 | + }); |
| 27 | +</script> |
| 28 | + |
| 29 | +<h3>Add shareholder</h3> |
| 30 | + |
| 31 | +{#if $message} |
| 32 | + <div |
| 33 | + class="status" |
| 34 | + class:error={$page.status >= 400} |
| 35 | + class:success={$page.status == 200} |
| 36 | + > |
| 37 | + {$message} |
| 38 | + </div> |
| 39 | +{/if} |
| 40 | + |
| 41 | +<form method="POST" use:enhance> |
| 42 | + <label> |
| 43 | + Business name |
| 44 | + <input bind:value={$form.business_id} /> |
| 45 | + {#if $errors.business_id}<span class="invalid" |
| 46 | + >{$errors.business_id}</span |
| 47 | + >{/if} |
| 48 | + </label> |
| 49 | + |
| 50 | + <button |
| 51 | + type="button" |
| 52 | + on:click={() => |
| 53 | + ($form.shareholders = [ |
| 54 | + ...$form.shareholders, |
| 55 | + { id_issuance_date: new Date() } |
| 56 | + ])}>Add shareholder</button |
| 57 | + > |
| 58 | + |
| 59 | + {#if $errors?.shareholders?._errors} |
| 60 | + <div class="invalid">{$errors.shareholders._errors}</div> |
| 61 | + {/if} |
| 62 | + |
| 63 | + {#each $form.shareholders as _, i} |
| 64 | + {@const error = $errors?.shareholders?.[i]?.id_issuance_date} |
| 65 | + <label> |
| 66 | + Issuance date<br /> |
| 67 | + <input |
| 68 | + aria-invalid={error ? 'true' : undefined} |
| 69 | + bind:value={$form.shareholders[i].id_issuance_date} |
| 70 | + /> |
| 71 | + {#if error} |
| 72 | + <span class="invalid" |
| 73 | + >{$errors?.shareholders?.[i].id_issuance_date}</span |
| 74 | + > |
| 75 | + {/if} |
| 76 | + </label> |
| 77 | + {/each} |
| 78 | + |
| 79 | + <div> |
| 80 | + <br /> |
| 81 | + <button>Submit</button> |
| 82 | + </div> |
| 83 | +</form> |
| 84 | + |
| 85 | +<SuperDebug data={{ $form, $errors }} /> |
| 86 | + |
| 87 | +<style> |
| 88 | + .invalid { |
| 89 | + color: red; |
| 90 | + } |
| 91 | +
|
| 92 | + .status { |
| 93 | + color: white; |
| 94 | + padding: 4px; |
| 95 | + padding-left: 8px; |
| 96 | + border-radius: 2px; |
| 97 | + font-weight: 500; |
| 98 | + } |
| 99 | +
|
| 100 | + .status.success { |
| 101 | + background-color: seagreen; |
| 102 | + } |
| 103 | +
|
| 104 | + .status.error { |
| 105 | + background-color: #ff2a02; |
| 106 | + } |
| 107 | +
|
| 108 | + input { |
| 109 | + background-color: #ddd; |
| 110 | + } |
| 111 | +
|
| 112 | + a { |
| 113 | + text-decoration: underline; |
| 114 | + } |
| 115 | +
|
| 116 | + hr { |
| 117 | + margin-top: 4rem; |
| 118 | + } |
| 119 | +
|
| 120 | + form { |
| 121 | + padding-top: 1rem; |
| 122 | + padding-bottom: 1rem; |
| 123 | + } |
| 124 | +</style> |
0 commit comments