implements max holder and validation on UI

This commit is contained in:
Julien Fastré 2021-06-11 17:58:09 +02:00
parent af740fd87d
commit 807d3674fc
8 changed files with 50 additions and 38 deletions

View File

@ -50,8 +50,12 @@ class HouseholdMemberController extends ApiController
// TODO ACL
//
// TODO validation
//
$errors = $editor->validate();
if (count($errors) > 0) {
return $this->json($errors, 422);
}
$em = $this->getDoctrine()->getManager();
// if new household, persist it

View File

@ -19,7 +19,7 @@ use Chill\PersonBundle\Validator\Constraints\Household\MaxHolder;
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
* "household"=Household::class
* })
* @MasHolder()
* @MaxHolder(groups={"memberships"})
*/
class Household
{

View File

@ -21,7 +21,7 @@ class MembersEditor
public function __construct(ValidatorInterface $validator, ?Household $household)
{
$this->validation = $validator;
$this->validator = $validator;
$this->household = $household;
}
@ -92,7 +92,7 @@ class MembersEditor
public function validate(): ConstraintViolationListInterface
{
return $this->validator->validate($this->getHousehold(), null, [ "memberships" ]);
}
public function getPersistable(): array
@ -100,6 +100,7 @@ class MembersEditor
return $this->persistables;
}
public function getHousehold(): ?Household
{
return $this->household;

View File

@ -16,32 +16,13 @@ const householdMove = (payload) => {
if (response.ok) {
return response.json();
}
throw Error('Error with testing move');
});
};
const householdMoveTest = (payload) => {
const url = `/api/1.0/person/household/members/move/test.json`;
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
.then(response => {
if (response.status === 422) {
return response.json();
}
if (response.ok) {
// return an empty array if ok
return new Promise((resolve, reject) => resolve({ violations: [] }) );
}
throw Error('Error with testing move');
});
};
export {
householdMove,
householdMoveTest
};

View File

@ -12,6 +12,9 @@
<li v-for="(msg, index) in warnings">
{{ $t(msg.m, msg.a) }}
</li>
<li v-for="msg in errors">
{{ msg }}
</li>
</ul>
<ul class="record_actions sticky-form-buttons">
@ -34,8 +37,9 @@ export default {
computed: {
...mapState({
warnings: (state) => state.warnings,
hasNoWarnings: (state) => state.warnings.length === 0,
hasWarnings: (state) => state.warnings.length > 0,
errors: (state) => state.errors,
hasNoWarnings: (state) => state.warnings.length === 0 && state.errors.length === 0,
hasWarnings: (state) => state.warnings.length > 0 || state.errors.length > 0,
}),
},
methods: {

View File

@ -26,6 +26,7 @@ const store = createStore({
allowLeaveWithoutHousehold: window.household_members_editor_data.allowLeaveWithoutHousehold,
forceLeaveWithoutHousehold: false,
warnings: [],
errors: []
},
getters: {
isHouseholdNew(state) {
@ -162,6 +163,9 @@ const store = createStore({
setWarnings(state, warnings) {
state.warnings = warnings;
},
setErrors(state, errors) {
state.errors = errors;
},
},
actions: {
addConcerned({ commit, dispatch }, person) {
@ -216,19 +220,33 @@ const store = createStore({
commit('setWarnings', warnings);
},
confirm({ getters, state }) {
confirm({ getters, state, commit }) {
let payload = getters.buildPayload,
errors = [],
person_id,
household_id;
householdMove(payload).then(household => {
if (household === null) {
person_id = getters.persons[0].id;
window.location.replace(`/fr/person/${person_id}/general`);
} else {
household_id = household.id;
// nothing to do anymore here, bye-bye !
window.location.replace(`/fr/person/household/${household_id}/members`);
}
household_id,
error
;
householdMove(payload).then(household => {
if (household === null) {
person_id = getters.persons[0].id;
window.location.replace(`/fr/person/${person_id}/general`);
} else {
if (household.type === 'household') {
household_id = household.id;
// nothing to do anymore here, bye-bye !
window.location.replace(`/fr/person/household/${household_id}/members`);
} else {
// we assume the answer was 422...
error = household;
for (let e in error.violations) {
errors.push('TODO');
}
commit('setErrors', errors);
}
}
});
},
}

View File

@ -12,4 +12,8 @@ class MaxHolder extends Constraint
public $message = 'household.max_holder_overflowed';
public $messageInfinity = 'household.max_holder_overflowed_infinity';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}

View File

@ -31,7 +31,7 @@ class MaxHolderValidator extends ConstraintValidator
if ($covers->hasIntersections()) {
foreach ($covers->getIntersections() as list($start, $end, $ids)) {
$msg = $end === null ? $constraint->messageEndInfinite :
$msg = $end === null ? $constraint->messageInfinity :
$constraint->message;
$this->context->buildViolation($msg)