alexpi Posted June 27, 2014 Share Posted June 27, 2014 Hello, I developed a guestbook for a hotel site, that creates a page with a name and a small message for each person who writes. I used the following code which I found in these forums, and it works but only if the page name is written in English. $name = $wire->input->post['guest_name']; $message = $wire->input->post['guest_message']; $p = new Page(); // create new page object $p->template = 'testimonial'; // set template $p->parent = wire('pages')->get('/guestbook/'); // set the parent $p->name = $wire->sanitizer->text($name); // give it a name used in the url for the page $p->body = $wire->sanitizer->textarea($message); $p->status = Page::statusUnpublished; $p->save(); If a guest writes a name in Greek for example, I get the following error: Fatal error: Uncaught exception 'WireException' with message 'Can't save page 0: /guestbook//: It has an empty 'name' field' in (etc) The strange thing is that the textarea part can be in any language. Isn't $sanitizer->text() safe for any character sets? Link to comment Share on other sites More sharing options...
apeisa Posted June 27, 2014 Share Posted June 27, 2014 That is bad example you have there. For pagename you need to use $sanitizer->pageName() method. But it's also possible in newer versions to let pw handle the name field, just give a title and pw transforms it to a unique name (and adds number suffix if required). 1 Link to comment Share on other sites More sharing options...
alexpi Posted June 28, 2014 Author Share Posted June 28, 2014 Is it possible to have page names with non-latin characters? I tried renaming a page to contain Greek characters in PWs admin, but It wasn't allowed. In my particular case all I needed was the page title, not a url, so I used a random number to store page names. $name = $wire->input->post['guest_name']; $message = $wire->input->post['guest_message']; $p->name = rand(); $p->title = $wire->sanitizer->text($name); $p->body = $wire->sanitizer->textarea($message); I am not sure if this code is good, but it works. Link to comment Share on other sites More sharing options...
LostKobrakai Posted June 29, 2014 Share Posted June 29, 2014 Just setting the title of the page should work, as processwire converts the title to the name, like it does in the backend. Using rand() isn't a good idea, because you easily could get duplications, where the page just isn't saved and throws an error. If you do something like this you always need to check for duplications and try again a few times, if there is a page with this name already, before saving it. 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