# Form ๋‹ค๋ฃจ๊ธฐ

ํผ(Form)์€ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ ํ˜•์‹์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ์ด๋‚˜ ์ƒํ’ˆ ๊ฒฐ์ œ ๋“ฑ ๋ชจ๋“  ๊ณณ์— ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

# Form ์ œ์ž‘ํ•˜๊ธฐ

๊ทธ๋Ÿผ ๋ทฐ๋กœ ๊ฐ„๋‹จํ•œ ๋กœ๊ทธ์ธ ํผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

# 1. HTML ํƒœ๊ทธ ๊ตฌ์„ฑ

๋จผ์ € ๊ฐ„๋‹จํ•œ HTML ํƒœ๊ทธ๋ถ€ํ„ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

<form>
  <div>
    <label for="email">Email</label>
    <input id="email" type="text">
  </div>
  <div>
    <label for="password">Password</label>
    <input id="password" type="password">
  </div>
  <div>
    <button type="submit">Login</button>
  </div>
</form>

# 2. HTML ํƒœ๊ทธ์— ๋ทฐ ์†์„ฑ ์ถ”๊ฐ€

์œ„์—์„œ ์ž‘์„ฑํ•œ ํผ์˜ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

ย 


ย 



ย 






<form v-on:submit.prevent="loginUser">
  <div>
    <label for="email">Email</label>
    <input id="email" type="text" ref="email">
  </div>
  <div>
    <label for="password">Password</label>
    <input id="password" type="password" ref="password">
  </div>
  <div>
    <button type="submit">Login</button>
  </div>
</form>

๊ฐ ์ธํ’‹ ๋ฐ•์Šค์˜ ์ž…๋ ฅ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ref ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์˜€๊ณ , ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด v-on:submit ๋””๋ ‰ํ‹ฐ๋ธŒ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. .prevent๋Š” ํผ์˜ ๊ธฐ๋ณธ์ ์ธ ๋™์ž‘์„ ๋ง‰๊ธฐ ์œ„ํ•œ modifier๋กœ์จ ํ”ํžˆ ์‚ฌ์šฉํ•˜๋Š” event.preventDefault() ์ฝ”๋“œ์™€ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

# 3. ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ์˜ ์ฒ˜๋ฆฌ

๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด loginUser() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

์ด๋ฉ”์ผ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฐ’์„ ๋ฐ›์•„ HTTP POST ์š”์ฒญ์„ ๋‚ ๋ ค์„œ ๋กœ๊ทธ์ธ ์ธ์ฆ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค.

# Form Validation

ํผ์—์„œ ๊ผญ ํ•ด์ค˜์•ผ ํ•˜๋Š” ์ž‘์—…์€ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์ด ์œ ํšจํ•œ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋ทฐ์—์„œ๋Š” vee-validate (opens new window) Vuelidate (opens new window) ๋“ฑ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํผ์˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ด์™ธ์—๋„ ๋ทฐ์˜ ๊ธฐ๋ณธ ์†์„ฑ์„ ํ™œ์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ์ธํ’‹ ๋ฐ•์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค.

<div>
  <label for="email">email</label>
  <input id="email" v-model="email" type="text">
</div>

์ธํ’‹ ๋ฐ•์Šค์˜ ์ž…๋ ฅ ๊ฐ’์€ ๋ชจ๋‘ v-model ๋””๋ ‰ํ‹ฐ๋ธŒ๋ฅผ ์ด์šฉํ•˜์—ฌ email ์ด๋ผ๋Š” ๋ทฐ ์ธ์Šคํ„ด์Šค ๋ฐ์ดํ„ฐ์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ด ์ธํ’‹ ๋ฐ•์Šค์— ์ตœ์†Œ 10๊ธ€์ž ์ด์ƒ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?

<div>
  <label for="email">email</label>
  <input id="email" v-model="email" type="text">
  <small>{{ emailValidation }}</small>
</div>

์œ„์™€ ๊ฐ™์ด ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ธ€์ž๊ฐ€ 10๊ฐœ ์ดํ•˜๋ฉด ๊ณ„์† ๊ฒฝ๊ณ  ํ‘œ์‹œ๊ฐ€ ๋œจ๋Š” computed ์†์„ฑ์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 10๊ธ€์ž ์ดํ•˜์ผ ๋•Œ๋Š” ๊ณ„์† <small> ํƒœ๊ทธ ์•ˆ์— ๊ธ€์ž์˜ ๊ธธ์ด๊ฐ€ 10 ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์•ˆ๋‚ด ๋ฌธ์ž๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.