useSubmitHandler-T2xFZY5q.js 994 B

1
  1. import{k as v,R as p}from"./index-Cd2JsgaO.js";function g(y={}){const{onSuccess:i,onError:c,formRef:f,scrollFn:l,containerRef:u}=y,r=v(!1),a=v({}),d=()=>{const t=f?.value;return t?Array.isArray(t)?t:[t]:[]},w=async()=>{if(!f?.value||!l)return;await p();const t=d();for(const o of t){if(!o)continue;const n=o.getValidationComponents();if(!n)continue;const e=n.find(s=>s.hasError);if(e){const s=u?.value||u;e.focus(),l(e,s);return}}},m=async t=>{r.value=!0,a.value={};let o=!0;const n=d();if(n.length>0)for(const e of n)e&&(await e.validate(!0)||(o=!1));if(!o)throw r.value=!1,await w(),new Error("Frontend validation failed.");try{const e=await t();if(typeof i=="function")await i(e);else return e}catch(e){await h(e)}finally{r.value=!1}},h=async t=>{if(t?.response?.status===422){const o=t.response.data.errors||{};for(const n in o){const e=o[n][0];a.value[n]=e}await w()}if(await p(),typeof c=="function")await c(t);else throw t};return{loading:r,validationErrors:a,execute:m}}export{g as u};