Martin Muzatko Posted April 25, 2017 Share Posted April 25, 2017 Hello! I'm trying to use the data I create in Processwire as much as possible. So for a form, I try to use the fields description, name and also its built-in validation rules I defined in ProcessWire on the front-end. (minlength, ranges, patterns, etc) I already looked into this tutorial, but it is using external resources to validate the form. Since ProcessWire does all the heavy lifting, when processing data, I don't have to sanitize anything - ___processInput should do the job just fine. However, it is not actually working correctly. $fields = $templates->get('user')->fields; $submission = $input->post; foreach ($submission as $key => $value) { $field = $fields->{$key}; if ($field instanceof Field) { $field = $field->getInputfield($user); $field->___processInput(new WireInputData([$key => $value])); var_dump($field->getErrors(true)); // retrieve validation error } } This works for some constraints, but the values are not correctly validated. Example: All the fields are required and zip is an integer field. Yet, I get no validation error for zip, although it was entered as a string, and not an integer. Funny enough: if I provide a number outside the range, I get "Specified value 2 removed because it is out of bounds (min=1000, max=99999)". firstname will not return any error for being a required field. From what I have looked through the source code, there is no check for "required". Some fields only validate on setAttribute. Am I missing anything or am I doomed to build my own validation process? Thank you in advance! Best, Martin Link to comment Share on other sites More sharing options...
Martin Muzatko Posted April 26, 2017 Author Share Posted April 26, 2017 I've read a lot into this tutorial, which uses the built-in validation: Thank you a lot for that @Soma! Although, CSRF does not work correctly, so I read through this topic here: But I can't find a clue, why when ajax-posting to my form, this fails. Link to comment Share on other sites More sharing options...
matjazp Posted April 26, 2017 Share Posted April 26, 2017 18 minutes ago, Martin Muzatko said: But I can't find a clue, why when ajax-posting to my form, this fails. Related? https://github.com/processwire/processwire-issues/issues/250 1 Link to comment Share on other sites More sharing options...
Martin Muzatko Posted April 26, 2017 Author Share Posted April 26, 2017 @matjazp I'm not sure. I made it now work with the following: $data = new WireInputData([ 'email' => $input->post->email, 'username' => $input->post->username, 'species' => $input->post->species, 'firstname' => $input->post->firstname, 'lastname' => $input->post->lastname, 'password' => $input->post->password, 'password_repeat' => $input->post->password_repeat, 'email' => $input->post->email, 'street' => $input->post->street, 'zip' => $input->post->zip, 'city' => $input->post->city, 'country' => $input->post->country, 'birthday' => $input->post->birthday ]); $token = $session->CSRF->getTokenName(); $data->$token = $session->CSRF->getTokenValue(); $post = $input->post; $post->setArray(array_merge($data->getArray(), $post->getArray())); Link to comment Share on other sites More sharing options...
abdus Posted April 26, 2017 Share Posted April 26, 2017 3 hours ago, Martin Muzatko said: $token = $session->CSRF->getTokenName(); $data->$token = $session->CSRF->getTokenValue(); You shouldn't use the actual token value you get from the session, you must use the value from the guest. The whole premise of CSRF (cross site request forgery) protection is to detect requests with invalid/missing tokens, so you know they're originated from a form on your site. If you don't use the posted value (a field starting with TOKEN in $input->post and its value that is sent with the request) you're practically removing CSRF protection altogether. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now