Change form valid method names

This commit is contained in:
Grant McConnaughey 2017-07-18 11:20:59 -05:00
parent 507246468b
commit f5083cb190
2 changed files with 13 additions and 9 deletions

View File

@ -60,7 +60,7 @@ Form validation
Form mutations will call ``is_valid()`` on your forms.
If the form is valid then ``perform_mutate(form, info)`` is called on the mutation. Override this method to change how
If the form is valid then ``form_valid(form, info)`` is called on the mutation. Override this method to change how
the form is saved or to return a different Graphene object type.
If the form is *not* valid then a list of errors will be returned. These errors have two fields: ``field``, a string

View File

@ -64,19 +64,23 @@ class BaseFormMutation(graphene.Mutation):
form = cls.get_form(root, args, context, info)
if form.is_valid():
return cls.perform_mutate(form, info)
return cls.form_valid(form, info)
else:
errors = [
ErrorType(field=key, messages=value)
for key, value in form.errors.items()
]
return cls(errors=errors)
return cls.form_invalid(form, info)
@classmethod
def perform_mutate(cls, form, info):
def form_valid(cls, form, info):
form.save()
return cls(errors=[])
@classmethod
def form_invalid(cls, form, info):
errors = [
ErrorType(field=key, messages=value)
for key, value in form.errors.items()
]
return cls(errors=errors)
@classmethod
def get_form(cls, root, args, context, info):
form_data = args.get(cls._meta.input_field_name)
@ -151,7 +155,7 @@ class ModelFormMutation(six.with_metaclass(ModelFormMutationMeta, BaseFormMutati
errors = graphene.List(ErrorType)
@classmethod
def perform_mutate(cls, form, info):
def form_valid(cls, form, info):
obj = form.save()
kwargs = {cls.return_field_name: obj}
return cls(errors=[], **kwargs)