horst

Field set to unique in table

Recommended Posts

Hi, I have the need to set a field to unique as described here (point 2.)

It is a integer input within a repeater. I have set that field to match a minimum and maximum value and also input is required!

When trying to save the page with an already used value the page doesn't save that field and do not output any error or warning. It silently lost the data, even with debugging enabled in config.php.

post-1041-0-52743900-1364845371_thumb.jp

What can I do to avoid this?

Share this post


Link to post
Share on other sites

Uniques aren't officially supported by that fieldtype, so it's a bit of an unknown. But I would think we should be seeing an exception thrown (and error message at the top of your screen). Do you see anything related in your /site/assets/logs/errors.txt? Can you try switching back to the default admin theme, just in case something about the theme is causing an error to be suppressed. 

Share this post


Link to post
Share on other sites

Hi Ryan,

  • Using the default AdminTheme with english language displays an ErrorMessage at the Top of the page: Duplicate entry '1002' for key 'data'
  • Using the default AdminTheme with the german language pack do display nothing!
  • Using the Metro AdminTheme with the german language pack do display nothing!
  • Using the Metro AdminTheme with english language do display nothing, too!

Other Warnings/Errors, like 'Missing required value ...', 'Value is out of bounds ...' are always displayed as expected.
 
There are no entries in the Error-Logfile with this.

Share this post


Link to post
Share on other sites
Using the default AdminTheme with english language displays an ErrorMessage at the Top of the page: Duplicate entry '1002' for key 'data'

This is what I'd expect to see. 

Using the default AdminTheme with the german language pack do display nothing!

This is kind of a mystery, as the language pack shouldn't make any difference here. I'll have to experiment and see what's up. 

Using the Metro AdminTheme with english language do display nothing, too!

I was going to go take a look at the MetroWire theme, but looks like it's no longer available for download. I'm getting a 404 when clicking the download link:

http://modules.processwire.com/modules/metro-wire/

  • Like 1

Share this post


Link to post
Share on other sites

This is what I'd expect to see. 

  

This is kind of a mystery, as the language pack shouldn't make any difference here. I'll have to experiment and see what's up.

Ryan, did you find out something about that with the use of the language pack?

I'm fine with the default admin theme, but language pack would be fine.

Share this post


Link to post
Share on other sites

I've looked into this, and don't think it's got anything to do with use of a language pack or admin theme. I tried executing the query manually in PhpMyAdmin and MySQL simply doesn't trigger an error when inserting duplicate values. It just skips the query. Maybe there are some conditions under which it will report an error, but not that I could find. I think the problem is that the query for inserting/updating fields looks like this:

INSERT INTO `field_test_unique` (pages_id, data) VALUES('17562', 'my-unique-value') ON DUPLICATE KEY UPDATE data=VALUES(data)

The "ON DUPLICATE KEY" part is where it's at. But we need that, otherwise we'd have to add another select to every insert/update just to see if something is already present. So it looks like any future fieldtype that supports a UNIQUE index would have to implement it's own savePageField() method with some more overhead to check things out and decide whether to insert or update. As a result, you can have a UNIQUE index right now, but it's just being enforced rather than reported. But making a new FieldtypeTextUnique or something like that would be a way to solve it pretty easily. 

  • Like 1

Share this post


Link to post
Share on other sites

...

So it looks like any future fieldtype that supports a UNIQUE index would have to implement it's own savePageField() method with some more overhead to check things out and decide whether to insert or update.

...

But making a new FieldtypeTextUnique or something like that would be a way to solve it pretty easily.

Thanks for looking to this Ryan.

So may I go to the wishlist and add a 'FieldtypeTextUnique' to it, or is it allready on a list you have? :-)

Share this post


Link to post
Share on other sites

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^:ph34r:  ^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

(but wait! there is someone acting very suspicious...)

  • Like 6

Share this post


Link to post
Share on other sites
On 18.4.2013 at 8:19 PM, diogo said:

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^prost.gif^-^^-^^-^^-^^-^ 

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

^-^^-^^-^^-^^-^^-^^-^^-^^-^

(but wait! there is someone acting very suspicious...)

No, this isn't suspicious, it's me saying skoal.

  • Like 8

Share this post


Link to post
Share on other sites

Can we have Unique field setting same as Required under Input tab on any field? The reason is that if I need to have an email address or number type or other built-in field type then I cannot have unique and if I have to apply unique to the field then I have to use FieldtypeTextUnique field type and cannot utilize built-in type.

  • Like 1

Share this post


Link to post
Share on other sites

This option may be coming in the future. 

  • Like 1

Share this post


Link to post
Share on other sites

Since this is a calculated comparison, I would love a feature where uniqueness starts after the language tree. So that i can have unique fields within a language but I can still copy the whole language tree and copy the "unique" fields over.

/de/file-with-uniquefield#1

/en/file-with-uniquefield#1

Share this post


Link to post
Share on other sites

Actually you can build these functionality by using a Repeaterfield that holds your fields for en + de.

Share this post


Link to post
Share on other sites

Dear Ryan,

I'm using TextUnique on a field called 'account_name', and process that field through the API.

I don't check for duplicate values in the code, because I figured that the field type would throw an error.

However, instead of an error, the field value is set to blank whenever a duplicate value is typed in,

and the user hits save.

Does the uniqueness check require special API code as well?

Thanks,

Peter

Share this post


Link to post
Share on other sites

Dear Ryan,

I added this code to my php file that saves the values, and it worked. Is there a different and better way to interact with textunique fields, via the API, or is the following code a good option?

            if ( $field->type == 'FieldtypeTextUnique' )
                  {
                  $field_value = $sanitizer->text($input->post->$field_name);

                  $check_field_dupe = '';
                  $check_field_dupe = $pages->get( "id!=$current_page_id,$field_name=$field_value" );

                  if ( $check_field_dupe->id )
                        {
                        $field_value_error  = 'yes';

                        $field_error_text .= "<span class='error_text'>
                                              '$field_value' is not available.<br>
                                              Please try something different.
                                              </span><br>
                                             ";
                        }
                  }

 

Thanks,

Peter

Share this post


Link to post
Share on other sites

Dear Ryan,

Another update.

My code worked above, against normal pages. That is, the code check for dupes worked against "non-deleted" pages.

However, after saving a value that was not a dupe in non-deleted pages, the value was still erased by the text-unique field,

after saving with the API call.

I discovered that the conflicting value still existed in a page in the Trash.

So, 'textunique' is included pages in the Trash, which should not happen, in my opinion.

Yours,

Peter

Share this post


Link to post
Share on other sites

$pages->get() implies an "include=all". You'd want to retrieve your pages with a function that filters things. Try replacing your get() with a find()->first(); and I think that'll give you the results you want. 

$check_field_dupe = $pages->find( "id!=$current_page_id, $field_name=$field_value, include=hidden, check_access=0" ); 
if(count($check_field_dupe)) {
  // you found a duplicate that's not in the trash
}

Or you could continue to use get(), but check if it's in the trash after retrieving it: 

$check_field_dupe = $pages->get( "id!=$current_page_id, $field_name=$field_value" );
if($check_field_dupe->id && !$check_field_dupe->isTrash()) {
  // you found a duplicate that's not in the trash
}

Share this post


Link to post
Share on other sites

Dear Ryan,

I've been searching for the syntax for find()->first() and couldn't find it. I noted that your code above is:

$check_field_dupe = $pages->find( "id!=$current_page_id, $field_name=$field_value, include=hidden, check_access=0" );

... even though you mentioned find()->first(). I searched through the wire directory and couldn't find that usage.

Does your find() line above, with the hidden and check_access params, not include the trash? If so, that would work, I think.

Or if there's a param like "not isTrash()" that would be good too.

On your second example [!$check_field_dupe->isTrash()] -- I would think that if there were more than one page returned, in the array,

that we'd have to use a loop to check if each was in the trash. Is that correct?

I guess you're also confirming that the textunique field does indeed require additional API code, unlike the email field, for example.

Thanks for your help,

Peter

Share this post


Link to post
Share on other sites

Dear Ryan,

Also, am I correct in thinking that deleted user accounts do not go into the trash?

I using the same type of API call to check for duplicate user names and emails.

Thanks,

Peter

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By MoritzLost
      This will be more of a quick tip, and maybe obvious to many of you, but it's a technique I found very useful when building display options. By display options I mean fields that control how parts of the page are displayed on the frontend, for example background colors, sizing, spacing and alignment of certain elements. I'll also touch on how to make those options intuitive and comfortable to use for clients.
      It basically involves setting up option values that can be used directly in CSS classes or as HTML elements and mapping those to CSS styling (which can be quickly generated in a couple of lines using a pre-processor such as SASS). Another important aspect is to keep the option values seperate from their corresponding labels; the former can be technical, the latter should be semantically meaningful. The field type that lends itself to this this seperation of concerns is the Selectable Options field, the following examples mostly use this field type. Note that this module is part of the ProcessWire core, but not installed by default.
      The following examples all come from real projects I built (though some are slightly modified to better demonstrate the principle).
      #1: Headline levels & semantics
      For a project that had many pages with long texts, I used a Repeater field to represent sections of text. Each section has a headline. Those sections may have a hierarchical order, so I used a Selectable Option field to allow setting the headline level for each section (you can guess where this is going). The definition of the options looks something like this (those are all in the format value|label, with each line representing one option, see the blogpost above for details):
      h2|Section headline h3|Sub-section headline Of course, the PHP code that generates the corresponding HTML can use those values :
      // "sections" is the repeater field foreach ($page->sections as $section) { // create an h2 / h3 tag depending on the selected option (called headline_level here) echo "<{$section->headline_level->value}>{$section->headline}</{$section->headline_level->value}>"; echo $section->body; } That's a pretty obvious example, but there are two important takeaways:
      I only used two options. Just because there are six levels of headlines in HTML, doesn't mean those are all relevant to the client. The less options there are, the easier it is to understand them, so only the options that are relevant should be provided. In this case, the client had provided detailed, structured documents containing his articles, so I could determine how many levels of hierarchy were actually needed. I also started at h2, since there should be only one h1 per page, so that became it's own field separate from the repeater. The two options have a label that is semantically relevant to the client. It's much easier for a client who doesn't know anything about HTML to understand the options "Section headline" and "Sub-section headline" than "h2" and "h3". Sure, it can be cleared up in the field description, but this way it goes from something that's quickly explained to something that needs no explanation at all. #2: Image width and SASS
      In the same project, there was also an image section; in our layout, some images spanned the entire width of the text body, others only half of it. So again, I created an options field:
      50|Half width 100|Full width In this case, I expected the client to request different sizes at some point, so I wanted it to be extensible. Of course, the values could be used to generate inline styles, but that's not a very clean solution (since inline styled break the cascade, and it's not semantic as HTML should be). Instead, I used it to create a class (admittedly, this isn't strictly semantic as well):
      <img src="..." class="w-<?= $section->image_width->value ?>"> With pure CSS, the amount of code needed to write out those class definitions will increase linearly with the number of options. In SASS however, you only need a couple of lines:
      @each $width in (50, 100) { .w-#{$width}{ max-width: percentage($width/100); } } This way, if you ever need to add other options like 25% or 75%, you only need to add those numbers to the list in parenthesis and you're done. You can even put the definition of the list in a variable that's defined in a central variables.scss file. Something like this also exists in Bootstrap 4 as a utility, by the way.
      It also becomes easier to modifiy those all at once. For example, if you decide all images should be full-width on mobile, you only need to add that once, no need to throw around !important's or modify multiple CSS definitions (this is also where the inline styles approach would break down) :
      # _variables.scss $image-widths: (25, 50, 75, 100); $breakpoint-mobile: 576px; # _images.scss @import "variables"; @each $width in $image-widths { .w-#{$width}{ max-width: percentage($width/100); @media (max-width: $breakpoint-mobile) { max-width: 100%; } } } One important gotcha: It might be tempting to just use an integer field with allowed values between 10 - 100. In fact, the amount of SASS code would be identical with a @for-directive to loop throuh the numbers. But that's exactly what makes point-and-click page builders so terrible for clients: too many options. No client wants to manually set numerical values for size, position and margins for each and every element (looking at you, Visual Composer). In fact, having too many options makes it much harder to create a consistent layout. So in those cases, less is more.
      #3: Multiple options in one field
      Another example for repeatable page sections, this time for a two-column layout. The design included multiple variants regarding column-span and alignment. Using a 12-column grid, we needed a 6-6 split, a centered 5-5 split, a left-aligned 6-4 split and a right-aligned 4-6 split. I didn't want to litter the repeater items with options, so I decided to put both settings in one field (called something like Column layout) :
      center_6_6|6 / 6 (Centered) center_5_5|5 / 5 (Centered) left_6_4|6 / 4 (Left-aligned) right_4_6|4 / 6 (Right-aligned) As long as the value format is consistent, the individual options can be quickly extracted and applied in PHP:
      [$alignment, $width['left'], $width['right']] = explode('_', $section->column_layout->value); echo '<section class="row justify-content-' . $alignment . '">'; foreach (['left', 'right'] as $side) { echo '<div class="col-lg-' . $width[$side] . '">'; echo $section->get("body_{$side}"); echo '</div>'; } echo '</section>'; If you don't recognize the syntax in the first line, it's symmetric array destructuring, introduced in PHP 7.1. For older versions you can use list() instead. This example uses Bootstrap 4 grid classes and flexbox utility classes for alignment. The corresponding CSS can be quickly generated in SASS as well, check the Bootstrap source code for a pointer.
      Again, I'm limiting the options to what is actually needed, while keeping it extensible. With this format, I can easily add other column layouts without having to touch the code at all.
      #4: Sorting page elements
      A final example. In this case I was working on a template for reference projects that had three main content sections in the frontend: A project description, an image gallery and embedded videos (each using their own set of fields). The client requested an option to change the order in which those sections appeared on the page. Had I known this earlier, I maybe would have gone for a Repeater Matrix approach once again, but that would have required restructuring all the fields (and the corresponding code), so instead I used a Selectable Option field (labelled "Display order"). My approach is similar to the one from the last example:
      body_gallery_embeds|Description - Gallery - Videos body_embeds_gallery|Description - Videos - Gallery gallery_body_embeds|Gallery - Description - Videos gallery_embeds_body|Gallery - Videos - Description embeds_body_gallery|Videos - Description - Gallery embeds_gallery_body|Videos - Gallery - Description Since there are six possibilities to sort three items, this is the expected number of options. So I decided to include them all, even though some are probably never going to be used. I also tried to use a predictable order for the options (i.e. the options come in pairs, depending on what element is first). And here is the code used on the frontend:
      // render the template files for each section and store the result in an associative array $contents = [ 'body' => wireRenderFile('partials/_section-body.php', $page), 'gallery' => wireRenderFile('partials/_section-gallery.php', $page), 'embeds' => wireRenderFile('partials/_section-embeds.php', $page), ]; // e.g. 'gallery_body_embeds' => ['gallery', 'body', 'embeds']; $order = explode('_', $page->display_order->value); // echo the contents in the order defined by the option value foreach ($order as $item) { echo $contents[$item]; } You can see how it will be easy to add an additional section and integrate it into the existing solution. Though a fourth item would result in 4! = 24 possibilities to sort them, so at that point I'd talk to my client about which layouts they actually need 🙂
      Conclusion
      I always try to keep my code and the interfaces I create with ProcessWire extensible and intuitive. Those are a couple of solutions I came up with for projects at work. They are certainly not the only approach, and there is nothing super special about those examples, but I found that putting a little more effort into defining options with meaningful labels and using option values that I can use directly in my templates makes the result less verbose and more maintainable. Some or most of this tutorial may be immediately obvious to you, but if you made it this far, hopefully you got something out of it 🙂
      Feel free to share your own methods to create display options, or how you would've approached those problems differently. Thanks for reading!
    • By Marcel
      Hey all,
      I want to customize the mywebsite/processwire/profile page. It's almost all good but I want to get rid of the sections 'Admin Theme' and 'Language' so that the user can just set a new password. (see image)
      Do I have to modify the admin template or how can I do that? Because when I go tree>Admin>Profile there are no fields to add or remove.
      Or where can I find the php file?
      I would appreciate your help.

    • By JanPavelka
      Hi guys, I'm new here.
      I created custom field impressum_text in Processwire and now I need display this field in my theme. How can I please display this custom field in my theme (in file _main.php)?
      Can you help me, please?
      Thank you!
       

    • By dweeda
      How do I migrate a template with all its fields and values to a new site? Is their an export/import process?
    • By rareyush
      I am using module "FormTemplateProcessor" to get data in a pages but they all are unpublished and when I am trying to display them with relevent pages it not working
       
      page1 p1 p2 p3 page2 (FormTemplateProcessor) up1 (unpublished page and have p1 id on field knows as "ID") up2 (unpublished page and have p3 id on field knows as "ID") up3 (unpublished page and have p2 id on field knows as "ID") now here
      up1, ip2, up3 can be created by p1, p2 or p3 and I am saving respectively I'm saving their id in field which is being used by up1,up2,up3, etc template.
       
      now I want to show the data from unpublished pages which belongs to p1, p2 and p3 respectively
       
       
      anyone has experienced this or knows something which can help me out ?