Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Escaping commas in Enum()


Reply


3 Posts   567 Views

Avatar
mikhail

Community Member, 4 Posts

29 December 2011 at 11:52am

Edited: 29/12/2011 11:53am

"Enum('Value1, Value2, Value3')"

But what if I wanted:

"Enum('Value 1, and some Value 2', 'Value 2, and a little bit of Value 7', 'Value3, cake and pie')"

As far as I can tell there's no easy way to do this. I have tried escaping the commas in one string, tried passing it as multiple strings (which of course didn't work), even created this field with , ASCII commas in the hopes that it would write to DB in the raw format, but output in the proper encoding. This was probably the closest attempt at faking this, but the front-end fields are actually just outputting , as raw text.

Feel free to ignore this, it's more of an aesthetics question, I have for now replaced the commas with + that should hopefully carry the same meaning.

Avatar
kpolszewsk

Community Member, 2 Posts

19 December 2012 at 4:24am

You could always create a custom field called e.g. "MySweetEnum", containing something like this:

<?php
class MySweetEnum extends Enum
{

public function __construct ( $name )
{
$enum = array (
'Value 1, and some Value 2',
'Value 2, and a little bit of Value 7',
'Value3, cake and pie'
);

parent::__construct ( $name, $enum);
}

}

and later use it like that:

public static $db = array(
'SomeEnumField' => 'MySweetEnum'
);

Avatar
Spambanjo

Community Member, 17 Posts

31 October 2014 at 2:18am

Unfortunately mikhail's solution does not work for 3.1+