Jump to content

Field set to unique in table


horst
 Share

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?

Link to comment
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. 

Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
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
Link to comment
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? :-)

Link to comment
Share on other sites

  • 1 month later...

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
Link to comment
Share on other sites

  • 3 weeks later...

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

Link to comment
Share on other sites

  • 1 month later...

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

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

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

Link to comment
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
}
Link to comment
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

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.

  • Similar Content

    • By prestoav
      Hi all,
      I could have sworn I used to be able to use the site generic 'title' field as a sub field of a repeater field. However I've tried to do this on two 3.0.165 sites recently and, while it will add the title field in the repeater field setup, it wont save the repeater title sub field's content when the repeater is used in a page template and edited.
      It;'s not a big issue but I wondered if this was a known restriction?
    • By spercy16
      I was previously using individual fields to capture and output multiple cards onto my web page but after recently learning about repeaters would prefer to use this field type, as it will be a lot more user friendly for end users to create new cards. I tried to modify my code to grab the fields from the new Repeater field(s) but am getting this error message on the page:
      Here is the code I'm currently trying to use for the cards:
      <?php // https://processwire.com/api/arrays/ // check if the array of images has items foreach($page->tall_card as $card) { $count = 0; $count++; $options = array( 'quality' => 70 ); $image = $card->image; $img = $card->image->size(550, 400, $options); $cardHeading = $card->title; $cardBody = $card->plain_paragraph; $raised = $card->raised_amount; $goal = $card->goal_amount; $link = $card->link; if ($raised == 0 ) : $percent = 0; else: $percent = $raised_amount / $goal_amount * 100; endif; if ($percent <= 5) : $percent = 0; endif; ?> <span id="card<?php echo $count?>" class="card"> <img class="cardThumb" src="<?php echo $img->url; ?>" alt="<?php echo $image->description; ?> Thumbnail" /> <div class="cardBody"> <div class="cardText"> <h2><?php echo $cardHeading; ?></h2> <?php echo $cardBody; ?> </div> <div class="progressBarSection"> <progress class="progressBar" id="progressbar<?php echo $count; ?>" value="<?php echo $percent; ?>" max="100"></progress> <span class="raisedAmount"> $<?php echo $raised; ?> usd </span> <span class="goalAmount"> $<?php echo $goal; ?> usd </span> </div> <div class="primaryBtn"> <a href="https://www.paypal.com/donate?hosted_button_id= <?php echo $link; ?> &source=url"> <button> <i class="fas fa-donate"></i> Donate </button> </a> </div> </div> </span> <?php } ?> Thanks in advance for any help!
    • By spercy16
      Coming from Perch (CMS) to Processwire, the one major downside I've found is that every place I want to insert content into my pages requires a single field (be that an image, integer, string, etc.). This can be a real pain to setup and manage if you have a lot of pages, many of them with dozens of unique fields. For example, on the Projects page of a site I'm working on right now I have to have fields for donate_amount (1-15, meaning fifteen unique fields that I have to duplicate manually), goal_amount(1-15), card_body(1-15), etc. After using Perch for a while, the way their system works is simply easier. You instead create your fields in a single PHP template (for every unique section, like a card) and their CMS imports those fields into the CMS when you choose to use that card, then you just fill in the values. You can also choose to reuse that card multiple times and each one you add gives you the several fields you need to fill in. So you can easily add your 15 cards, each with a picture, title, paragraph, and link, etc. and each one is basically given a unique ID that you can reference in your CSS files. If there's any way to make fields and templates work more like that, it would be a truly outstanding and efficient CMS. The great advantage of the way Perch is setup is that I can make a card with 10 fields, if need be, and easily duplicate that card out 40 times if I need 40 cards on the page and wouldn't need 10 fields for 40 cards (meaning in PW I would need 400 fields). If there's an easier way to do this in PW I would love to know, but as of right now, from what little I do know about the CMS 400 fields would be the only way to set that up, and that would be much more time consuming than if I could create one card (in PHP) with 10 fields that automatically added a number to the end of each field for each card (if that makes sense).
    • By benbyf
      Never really got my head around field groups and it shows, In my module uninstall I think I figured out how to delete a page, then a template and it's fieldgroup so that nothing gets upset. But for fields that belong to templates that need to stoick around I'm still abit stuck to how you would remove the field and when I try I still get:
      Unable to delete field '[field name]' because it is in use by these fieldgroups: 'user'
      is there a tutorial or best method for removing added fields, pages and templates?
    • By nuel
      Dear PW Community
      Let me shout out my question here, I really don't know where to start and hope someone can give me a hint or tell me to resign and go home and cry.
      I want to create a subpage that is only accessible to people with unique access codes. It's gonna be an online concert streaming page (thanks Corona!). People who buy tickets through a local ticketing service should be able to access and stream the show with their individual access code. These codes should work only for this person and show. If someone in the «audience» closes and reopens the page, they should get in again, but not their friends who were given the code of course, basically just like in a club with a ticket and a stamp on the wrist.
      Now, is there a possibility to achieve that with more or less basic Processwire skills? In my imagination I have a field where I list the given access codes, another two to add start and ending date/time of the show, maybe one for a unique ID/title of the show.
      Is there an existing module for something like that? Should I get into the module development field and create that? How?? Haha. Any comments are welcome here.
      Thanks,
      Nuél
       
×
×
  • Create New...