I have a dynamic form.
For exemple, if something, my form contains FormType1 else my form contains FormType2 or FormType3 ....
I would detect if my form is modify.
If my form is completed, I apply is valid()
method and if there is an error I return in the form or if there is no error, I apply a redirection.
I know how to do that.
But, if my fom was submitted without modifications (with no new value) I want to apply an other redirection without validation because my validation will return false with required fields.
I can't test if all values are null because in form there may be combobox or boolean, or initial value, ...
I have a solution with javascript and two submit buttons.
If the value of button submit is 'just_redirection' I don't apply the validation else I apply validation.
Is there a way in Symfony2 (or full PHP) to know if the submitted form is the same that the initial form?
Let's assume your form is bound with an entity MyEntity
, in your controller you need to copy your original object, let the form handle the request, then compare your objects:
public function aRouteAction(Request $request, MyEntity $myEntity)
{
$entityFromForm = clone $myEntity;
// you need to clone your object, because since PHP5 a variable contains a reference to the object, so $entityFromForm = $myEntity wouldn't work
$form = $this->createForm(new MyFormType(), $entityFromForm);
$form->handleRequest($request);
if($entityFromForm == $myEntity) {
// redirect when no changes
}
if($form->isValid()) {
// redirect when there are changes and form submission is valid
}
else {
// return errors
}
}
For object cloning please refer to the php manual.