I wrote custom component, which is simple form. After submitting the form, I want to validate the form and in case of error, I want to display them. I tried to make a method with POST request mapping in my class MyFormComponentController extends AbstractAcceleratorCMSComponentController<MyFormComponentModel>
but it gets me an error saying that method is not allowed. So I wrote a controller and added action to my form component
@Controller
@RequestMapping(value = "/action")
public class MyFormPageController extends AbstractPageController {
@RequestMapping(method = RequestMethod.POST)
public String doMyPageController(@Valid final MyForm myForm, final BindingResult bindingResult, final Model model)
return ???; //..in case of error?
But when there is an error in my form and I wan't to return is back to the view, only component is displayed. I tried to pass there the value I got from getView()
method in my component controller, but it didn't help.
EDIT: I also tried to make form submit with ajax call, like:
$(document).on("submit",'#myForm', function(e){
e.preventDefault();
var postData = $("#myForm").serialize();
var url= $(".js-my-action").data("url");
$.ajax({
url: url,
data: JSON.stringify(postData),
type: "POST",
success: function (response){
}
});
})
and the method which I want to call is
@RequestMapping(value = "/action", method = RequestMethod.POST)
public String doMyPageController(@RequestBody @Valid final MyForm catalogOrderForm, final Model model) throws IOException, JaloBusinessException, CMSItemNotFoundException {
Here I tried to change @RequestBody
to @ModelAttribute
with a name of the form, but I only get Acces denied (CSRFToken)
error:
ERROR [hybrisHTTP24] [SikoAcceleratorAccessDeniedHandler] Access denied happend - org.springframework.security.web.csrf.InvalidCsrfTokenException: Invalid CSRF Token '66d045fd-c9ae-4210-ae97-f2a0e739bcbf"' was found on the request parameter 'CSRFToken' or header 'CSRFToken'.
The data which I'm passing in ajax call:
"firstName=Test&lastName=Test&street=Test&city=Test&postalCode=38801&email=test%40test.com&_inspiration=on&_kitchen=on&_door=on&_gdpr=on&CSRFToken=66d045fd-c9ae-4210-ae97-f2a0e739bcbf"
Then I tried to pass data in ajax call like:
data: {myForm: postData}
but it also didn't work.