Skip to content

Commit

Permalink
Change form valid method names
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant McConnaughey committed Jul 18, 2017
1 parent d7dca66 commit 6c5771e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/form-mutations.rst
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions graphene_django/forms/mutation.py
Expand Up @@ -64,19 +64,23 @@ def mutate(cls, root, args, context, info):
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)
Expand Down Expand Up @@ -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)

0 comments on commit 6c5771e

Please sign in to comment.