markus_blue_tomato Posted March 10, 2021 Share Posted March 10, 2021 Is there a way to have a unique Textfield? A Textfield which value is unique to all other pages with the same template? I found https://processwire.com/modules/fieldtype-text-unique/ but saw that it is not maintained for a long time. I had in my mind, that some others here had this issue already but my search did not result in anything.... 1 Link to comment Share on other sites More sharing options...
Robin S Posted March 10, 2021 Share Posted March 10, 2021 53 minutes ago, markus_blue_tomato said: I found https://processwire.com/modules/fieldtype-text-unique/ but saw that it is not maintained for a long time. Maybe there's nothing wrong with it that needs maintaining. Alternatively you could use an ordinary text field and use a hook to validate the inputfield value: $wire->addHookAfter('InputfieldText::processInput', function(HookEvent $event) { $inputfield = $event->object; $field = $inputfield->hasField; if($field && $field->name === 'text_unique') { // Look for existing page with the field value $value = $event->wire()->sanitizer->selectorValue($inputfield->value); $existing_page = $event->wire()->pages->get("text_unique=$value, template=news_item"); if($existing_page->id) { // Show an error message $inputfield->error("Value '{$inputfield->value}' already in use on page '{$existing_page->title}'."); // Clear value $inputfield->value = ''; } } }); 2 1 Link to comment Share on other sites More sharing options...
markus_blue_tomato Posted March 10, 2021 Author Share Posted March 10, 2021 Thanks! 1 Link to comment Share on other sites More sharing options...
markus_blue_tomato Posted March 10, 2021 Author Share Posted March 10, 2021 1 hour ago, Robin S said: Maybe there's nothing wrong with it that needs maintaining. I saw some issues with InnoDB (which I use) - maybe I will give it a try Link to comment Share on other sites More sharing options...
markus_blue_tomato Posted March 10, 2021 Author Share Posted March 10, 2021 Ah.... I have mistaken this with the "Unique" Page Name Status. 1 Link to comment Share on other sites More sharing options...
DrQuincy Posted April 1, 2021 Share Posted April 1, 2021 Thanks for posting this, I was about to as something similar. I'm not sure the “Unique” status is a good fix to what I'm after doing. It looks like a hook per @Robin S‘s code would do the job nicely. I love PW. One thing I do think would be nice though for a future version would be for the core to offer some of the data integrity features of a rational database but managed through template and page settings: Something to mimic CASCADE and RESTRICT so that in a page reference, for example, you can restrict it being deleted if another page references it Make a field (of fields) unique as above 1 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