Skip to content
Snippets Groups Projects
Commit 606d8a1d authored by Stephanie Briere Americo's avatar Stephanie Briere Americo
Browse files

Merge branch 'issue/28' into 'development'

Issue #28: Fix mouse being pushed in /create and /edit

See merge request !30
parents fc92fe85 ce67dc0f
No related branches found
No related tags found
3 merge requests!58Version 1.1,!54Issue #53: Fix password info,!30Issue #28: Fix mouse being pushed in /create and /edit
import * as Yup from "yup";
/** The validation through Yup is schema based, so there are schemas and it's validations. */
/** Schema to validate the question field from every form array object. */
const questionTextSchema = Yup.string()
.required("Este campo é obrigatório!")
.test("alphabets", "O caractere não é permitido", (value) => {
/**Function that compares the value with a regex, to assure the input is valid */
function checkText(value) {
return /^[A-Za-z!?$%,. 1234567890àèìòùáéíóúâêîôûãõç]+$/.test(value);
});
/** Function that applies the validation of it's used schema and sets the error messages. */
export async function testQuestionTextSchema(form, value, index) {
await questionTextSchema
.validate(value)
.then((x) => {
form[index].error.errorMsg.question = "";
})
.catch((err) => {
form[index].error.errorMsg.question = err.message;
});
}
/** Schema to validate the description field from every form array object. */
const descriptionTextSchema = Yup.string().test(
"alphabets",
"O caractere não é permitido",
(value) => {
if (!value) return true;
return /^[A-Za-z!?$%,. 1234567890àèìòùáéíóúâêîôûãõ\b- ]+$/.test(value);
/** Function that applies the validation of it's used schema and sets the error messages. */
export async function testQuestionTextSchema(error, value) {
value
? checkText(value)
? (error.errorMsg.question = "")
: (error.errorMsg.question = "O caractere não é permitido")
: (error.errorMsg.question = "Este campo é obrigatório!");
}
);
/** Function that applies the validation of it's used schema and sets the error messages. */
export async function testDescriptionTextSchema(form, value, index) {
await descriptionTextSchema
.validate(value)
.then((x) => {
form[index].error.errorMsg.description = "";
})
.catch((err) => {
form[index].error.errorMsg.description = err.message;
});
export async function testDescriptionTextSchema(error, value) {
value && checkText(value)
? (error.errorMsg.description = "")
: (error.errorMsg.description = "O caractere não é permitido");
}
/** Schema to validate the number of options at options field from FormFieldSelect, FormFieldCheckbox and FormFieldRadio. */
const selectOptionsSchema = Yup.array()
......@@ -87,23 +68,13 @@ export async function testSubformSchema(form, index) {
form[index].error.errorMsg.subformUsage = err.message;
});
}
/** Schema to validate the content of the options field from FormFieldSelect, FormFieldCheckbox and FormFieldRadio. */
const optSchema = Yup.string()
.required("Por favor, preencha esta opção")
.test("alphabets", "O caractere não é permitido", (value) => {
if (!value) return true;
return /^[A-Za-z!?$%,. àèìòùáéíóúâêîôûãõ\b- ]+$/.test(value);
});
/** Function that applies the validation of it's used schema and sets the error messages. */
export async function selectOptionTextTesting(form, value, index, idopt) {
await optSchema
.validate(value)
.then((x) => {
form[index].error.errorMsg.options[idopt] = "";
})
.catch((err) => {
form[index].error.errorMsg.options[idopt] = err.message;
});
export async function selectOptionTextTesting(error, value, idopt) {
value
? checkText(value)
? (error.errorMsg.options[idopt] = "")
: (error.errorMsg.options[idopt] = "O caractere não é permitido")
: (error.errorMsg.options[idopt] = "Por favor, preencha esta opção");
}
/** Schema to validate the quantity field of the validation from FormFieldText. */
const textValidationSchema = Yup.string()
......
......@@ -138,7 +138,7 @@ const useForm = () => {
*/
async function setQuestionField(value, index) {
form[index].question = value;
await testQuestionTextSchema(form, value, index);
testQuestionTextSchema(form[index].error, value);
setForm([...form]);
}
......@@ -148,7 +148,7 @@ const useForm = () => {
*/
async function setDescriptionField(value, index) {
form[index].description = value;
await testDescriptionTextSchema(form, value, index);
testDescriptionTextSchema(form[index].error, value);
setForm([...form]);
}
......@@ -157,9 +157,9 @@ const useForm = () => {
* @param index - the position on the array that the operation needs to be done;
* @param idopt - the id of the options being changed, inside the form[index].
*/
async function setSelectOption(value, index, idopt) {
function setSelectOption(value, index, idopt) {
form[index].options[idopt] = value;
await selectOptionTextTesting(form, value, index, idopt);
selectOptionTextTesting(form[index].error, value, idopt);
setForm([...form]);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment