Merge pull request #5787 from strapi/fix/password-type

Fix input type and add clear logic for this field type
This commit is contained in:
cyril lopez 2020-04-14 15:12:58 +02:00 committed by GitHub
commit e5d92f753a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 21 deletions

View File

@ -212,7 +212,7 @@ module.exports = {
'/3.0.0-beta.x/guides/scheduled-publication', '/3.0.0-beta.x/guides/scheduled-publication',
'/3.0.0-beta.x/guides/slug', '/3.0.0-beta.x/guides/slug',
'/3.0.0-beta.x/guides/send-email', '/3.0.0-beta.x/guides/send-email',
'/3.0.0-beta.x/guides/registering-field-in-admin', '/3.0.0-beta.x/guides/registering-a-field-in-admin',
'/3.0.0-beta.x/guides/count-graphql', '/3.0.0-beta.x/guides/count-graphql',
], ],
}, },

View File

@ -61,7 +61,15 @@ function Inputs({ autoFocus, keys, layout, name, onBlur }) {
const disabled = useMemo(() => !get(metadatas, 'editable', true), [metadatas]); const disabled = useMemo(() => !get(metadatas, 'editable', true), [metadatas]);
const type = useMemo(() => get(attribute, 'type', null), [attribute]); const type = useMemo(() => get(attribute, 'type', null), [attribute]);
const regexpString = useMemo(() => get(attribute, 'regex', null), [attribute]); const regexpString = useMemo(() => get(attribute, 'regex', null), [attribute]);
const validations = omit(attribute, [ const value = get(modifiedData, keys, null);
const temporaryErrorIdUntilBuffetjsSupportsFormattedMessage = 'app.utils.defaultMessage';
const errorId = get(
formErrors,
[keys, 'id'],
temporaryErrorIdUntilBuffetjsSupportsFormattedMessage
);
let validationsToOmit = [
'type', 'type',
'model', 'model',
'via', 'via',
@ -70,7 +78,20 @@ function Inputs({ autoFocus, keys, layout, name, onBlur }) {
'plugin', 'plugin',
'enum', 'enum',
'regex', 'regex',
]); ];
// Remove the required validation for the password type unless the form is already submitted
// So the error is properly displayed in the password input
if (type === 'password' && errorId !== 'components.Input.error.validation.required') {
validationsToOmit = [...validationsToOmit, 'required'];
}
// Remove the minLength validation when the user clears the input so it is not displayed
if (type === 'password' && !value) {
validationsToOmit = [...validationsToOmit, 'minLength'];
}
const validations = omit(attribute, validationsToOmit);
if (regexpString) { if (regexpString) {
const regexp = new RegExp(regexpString); const regexp = new RegExp(regexpString);
@ -81,17 +102,11 @@ function Inputs({ autoFocus, keys, layout, name, onBlur }) {
} }
const { description, visible } = metadatas; const { description, visible } = metadatas;
const value = get(modifiedData, keys, null);
if (visible === false) { if (visible === false) {
return null; return null;
} }
const temporaryErrorIdUntilBuffetjsSupportsFormattedMessage = 'app.utils.defaultMessage';
const errorId = get(
formErrors,
[keys, 'id'],
temporaryErrorIdUntilBuffetjsSupportsFormattedMessage
);
const isRequired = get(validations, ['required'], false); const isRequired = get(validations, ['required'], false);
if (type === 'relation') { if (type === 'relation') {

View File

@ -142,9 +142,13 @@ const EditViewDataManagerProvider = ({ allLayoutData, children, redirectToPrevio
}; };
const checkFormErrors = async (dataToSet = {}) => { const checkFormErrors = async (dataToSet = {}) => {
const schema = createYupSchema(currentContentTypeLayout, { const schema = createYupSchema(
currentContentTypeLayout,
{
components: get(allLayoutData, 'components', {}), components: get(allLayoutData, 'components', {}),
}); },
isCreatingEntry
);
let errors = {}; let errors = {};
const updatedData = cloneDeep(modifiedData); const updatedData = cloneDeep(modifiedData);
@ -186,6 +190,15 @@ const EditViewDataManagerProvider = ({ allLayoutData, children, redirectToPrevio
inputValue = null; inputValue = null;
} }
if (type === 'password' && !value) {
dispatch({
type: 'REMOVE_PASSWORD_FIELD',
keys: name.split('.'),
});
return;
}
// Allow to reset enum // Allow to reset enum
if (type === 'select-one' && value === '') { if (type === 'select-one' && value === '') {
inputValue = null; inputValue = null;
@ -207,9 +220,13 @@ const EditViewDataManagerProvider = ({ allLayoutData, children, redirectToPrevio
e.preventDefault(); e.preventDefault();
// Create yup schema // Create yup schema
const schema = createYupSchema(currentContentTypeLayout, { const schema = createYupSchema(
currentContentTypeLayout,
{
components: get(allLayoutData, 'components', {}), components: get(allLayoutData, 'components', {}),
}); },
isCreatingEntry
);
try { try {
// Validate the form using yup // Validate the form using yup

View File

@ -161,6 +161,9 @@ const reducer = (state, action) => {
return state.updateIn(componentPathToRemove, () => null); return state.updateIn(componentPathToRemove, () => null);
} }
case 'REMOVE_PASSWORD_FIELD': {
return state.removeIn(['modifiedData', ...action.keys]);
}
case 'REMOVE_REPEATABLE_FIELD': { case 'REMOVE_REPEATABLE_FIELD': {
const componentPathToRemove = ['modifiedData', ...action.keys]; const componentPathToRemove = ['modifiedData', ...action.keys];

View File

@ -56,7 +56,7 @@ yup.addMethod(yup.string, 'isSuperior', function(message, min) {
const getAttributes = data => get(data, ['schema', 'attributes'], {}); const getAttributes = data => get(data, ['schema', 'attributes'], {});
const createYupSchema = (model, { components }) => { const createYupSchema = (model, { components }, isCreatingEntry = true) => {
const attributes = getAttributes(model); const attributes = getAttributes(model);
return yup.object().shape( return yup.object().shape(
@ -68,7 +68,7 @@ const createYupSchema = (model, { components }) => {
attribute.type !== 'component' && attribute.type !== 'component' &&
attribute.type !== 'dynamiczone' attribute.type !== 'dynamiczone'
) { ) {
const formatted = createYupSchemaAttribute(attribute.type, attribute); const formatted = createYupSchemaAttribute(attribute.type, attribute, isCreatingEntry);
acc[current] = formatted; acc[current] = formatted;
} }
@ -166,7 +166,7 @@ const createYupSchema = (model, { components }) => {
); );
}; };
const createYupSchemaAttribute = (type, validations) => { const createYupSchemaAttribute = (type, validations, isCreatingEntry) => {
let schema = yup.mixed(); let schema = yup.mixed();
let regex = get(validations, 'regex', null); let regex = get(validations, 'regex', null);
@ -231,9 +231,18 @@ const createYupSchemaAttribute = (type, validations) => {
validationValue === 0 validationValue === 0
) { ) {
switch (validation) { switch (validation) {
case 'required': case 'required': {
if (type === 'password' && isCreatingEntry) {
schema = schema.required(errorsTrads.required); schema = schema.required(errorsTrads.required);
}
if (type !== 'password') {
schema = schema.required(errorsTrads.required);
}
break; break;
}
case 'max': { case 'max': {
if (type === 'biginteger') { if (type === 'biginteger') {
schema = schema.isInferior(errorsTrads.max, validationValue); schema = schema.isInferior(errorsTrads.max, validationValue);