Skip to content

Commit

Permalink
Edit tests form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SemaiCZE committed Dec 1, 2017
1 parent 179c8a7 commit b16ec8d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/components/forms/EditTestsForm/EditTests.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.testRow {
margin-bottom: -15px;
margin-bottom: 0;
margin-top: -20px;
}
45 changes: 44 additions & 1 deletion src/components/forms/EditTestsForm/EditTestsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,52 @@ EditTestsForm.propTypes = {
formValues: PropTypes.object
};

const validate = () => {
const validate = ({ isUniform, tests }) => {
const errors = {};

const testsErrors = {};
const knownTests = new Set();
for (let i = 0; i < tests.length; ++i) {
const test = tests[i];
const testErrors = {};
if (!test.name || test.name === '') {
testErrors['name'] = (
<FormattedMessage
id="app.editTestsForm.validation.testName"
defaultMessage="Please fill test name."
/>
);
}
if (knownTests.has(test.name)) {
testErrors['name'] = (
<FormattedMessage
id="app.editTestsForm.validation.testNameTaken"
defaultMessage="This name is taken, please fill different one."
/>
);
}
knownTests.add(test.name);
if (!isUniform && (!test.weight || test.weight === '')) {
testErrors['weight'] = (
<FormattedMessage
id="app.editTestsForm.validation.testWeightEmpty"
defaultMessage="Please fill test weight."
/>
);
}
const weight = Number.parseInt(test.weight);
if (!isUniform && (!Number.isFinite(weight) || weight < 0)) {
testErrors['weight'] = (
<FormattedMessage
id="app.editTestsForm.validation.testWeight"
defaultMessage="Test weight must be positive integer."
/>
);
}
testsErrors[i] = testErrors;
}
errors['tests'] = testsErrors;

return errors;
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/forms/EditTestsForm/EditTestsTestRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const EditTestsTestRow = ({ test, onRemove, isUniform }) =>
name={`${test}.name`}
component={TextField}
label={''}
className="testRow"
groupClassName="testRow"
/>
</td>
{!isUniform &&
Expand All @@ -24,7 +24,7 @@ const EditTestsTestRow = ({ test, onRemove, isUniform }) =>
name={`${test}.weight`}
component={TextField}
label={''}
className="testRow"
groupClassName="testRow"
/>
</td>}
<td style={{ verticalAlign: 'middle' }}>
Expand Down
5 changes: 4 additions & 1 deletion src/components/forms/Fields/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ const TextField = ({
meta: { touched, error },
type = 'text',
label,
groupClassName = '',
...props
}) =>
<FormGroup
controlId={input.name}
validationState={error ? (touched ? 'error' : 'warning') : undefined}
className={groupClassName}
>
<ControlLabel>
{label}
Expand Down Expand Up @@ -60,7 +62,8 @@ TextField.propTypes = {
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({ type: PropTypes.oneOf([FormattedMessage]) })
]).isRequired
]).isRequired,
groupClassName: PropTypes.string
};

export default TextField;
30 changes: 15 additions & 15 deletions src/pages/EditExerciseSimpleConfig/EditExerciseSimpleConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,22 @@ class EditExerciseSimpleConfig extends Component {
unlimitedHeight
>
<ResourceRenderer
resource={[...runtimeEnvironments.toArray()]}
resource={[
exerciseEnvironmentConfig,
...runtimeEnvironments.toArray()
]}
>
{(...environments) =>
<ResourceRenderer resource={exerciseEnvironmentConfig}>
{environmentConfigs =>
<EditEnvironmentSimpleForm
initialValues={getEnvInitValues(environmentConfigs)}
runtimeEnvironments={environments}
onSubmit={data =>
transformAndSendEnvValues(
data,
environments,
editEnvironmentConfigs
)}
/>}
</ResourceRenderer>}
{(environmentConfigs, ...environments) =>
<EditEnvironmentSimpleForm
initialValues={getEnvInitValues(environmentConfigs)}
runtimeEnvironments={environments}
onSubmit={data =>
transformAndSendEnvValues(
data,
environments,
editEnvironmentConfigs
)}
/>}
</ResourceRenderer>
</Box>
</Col>
Expand Down

0 comments on commit b16ec8d

Please sign in to comment.