Jump to content

Saving a page a with non-latin character name


alexpi
 Share

Recommended Posts

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

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).

  • Like 1
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...