Checking if an attribute handle exists

Permalink
I've looked around the forum and C5 source for this and found a few answers, but nothing that has actually worked.

I have a single page in the dashboard to add pages, and in the controller I need to validate all of the attribute handles recieved.

I have tried using :
$ak = CollectionAttributeKey::getByHandle('my_attribute_handle');
if ( is_null($ak) || !is_object($ak) || !intval($ak->getAttributeKeyID()) )


And just
$ak = CollectionAttributeKey::getByHandle('my_attribute_handle');
if (!is_object($ak))


But I get a different result if I send it a hard coded string like 'meta-title', which it will correctly say exists, but the moment I send it the exact same string that was retrieved from the sent data, it says the attribute handle doesn't exist. To be clear, the hard-coded value and the value retrieved from the form are both equal to 'meta-title'.

 
Hubble replied on at Permalink Reply
Still doesn't work actually, thought I had figured it out...
JohntheFish replied on at Permalink Reply
JohntheFish
If you are going to give yourself a 'best answer', please add some value to your post to help others in the future.
Hubble replied on at Permalink Reply
I thought I had solved my problem, so I marked it as best answer. I realized later that I hadn't solved the problem, but I don't think that there's a way to just unmark a response as best answer other than marking another response as best answer.

I should have probably put the reason for the edit!
mnakalay replied on at Permalink Reply
mnakalay
Could you share your code, both from the view where attributes are entered and from the controller where validation takes place? It's a bit hard to know what the problem is with the description you give.
Hubble replied on at Permalink Best Answer Reply
*EDIT:

The actual problem was that I had to do strval() on a string. Fun!

*Original
I finally found the solution. The problem was with the binary of the strings.

Instead of using what I hand found in another post (https://www.concrete5.org/community/forums/chat/check-if-attribute-handle-exist) I now use

public static function checkCollectionAttributeHandleExists($attributeHandleToCheck){
        $keyslist = CollectionKey::getList('Collection');
        foreach($keyslist AS $key){
            if(strcmp($key->getAttributeKeyHandle(), $attributeHandleToCheck) == 0){
                return true;
            }
        }
        return false;
    }


Definitely not the most efficient solution, but makes sure that it is a binary-safe check.