Best way to validate custom attributes

Permalink
I've created some extra user attributes and want to validate them, what would be the best way to do it?

Currently I'm using this, but it seems bloated and maybe there is a simpler approach?

$ak = UserAttributeKey::getByHandle($akHandle);
$postValue = $this->post('akID')[$ak->getAttributeKeyID()]['value']);
if($postValue == 'blabla'){
  // etc.
}

A3020
 
goldhat replied on at Permalink Reply
You could shorten a little by chaining the call to getAttributeKeyID() but I think you wanted something more revolutionary than that? Sorry I don't really see a way to shorten it if you only have the $akHandle you have to look up the key ID to get the value... well you could shorten it again just by skipping the pointless variable creation of $postValue.

A more out-of-the-box approach might be client-side validation is there a way to get jquery validation in there and use that?

$akID = UserAttributeKey::getByHandle($akHandle)->getAttributeKeyID();
if( $this->post('akID')[$akID]['value'] != "valid input" ) {
  // test input
}
A3020 replied on at Permalink Reply
A3020
Ok, yeah I was hoping for a more revolutionary solution ;)
For example, this code would probably give an error if the akHandle is not in de post request.

I guess a solution to this could be a helper like this:
$helper->post('ak_handle') and just returns the post value. The helper then needs to get the attribute key, check for an instance, check if the post parameters exist and return the value or null.

What do you think?
A3020 replied on at Permalink Reply
A3020
It seems there is a validateAttributeForm function in /core/models/attribute/key.php. It checks and runs the function 'validateForm' in an attribute type model. For example /core/models/attribute/types/select.php.

I still haven't found nice way to validate individual attributes though.