Revision #4 has been created by wei on Oct 25, 2010, 1:38:59 AM with the memo:
[...]In a very timely fashion the [jamselect](http://www.yiiframework.com/extension/jamselect/ "jamselect") widget showed up, thanks volkmar!
The widget does not provide min/max filtering so I will implement a custom rule.
First put the widget in a form
This is my implementation.
A few things to notice
The widget is located in
/protected/extensions not /protected/extensions/widgets
, I only point this out for people who aren't sure where to put things. You have quite a bit of flexibility on where you locate widgets and components. It doesn't makes sense to me to create another folder in extensions but perhaps on a very large project it would.
The dropdown list data is being pulled from a table that has only two fields,
, since I want to select the word from the list and store exactly the same value in the database I need a key=>value array that looks like
data will contain an array of selected values, this will be in
Lets look at the controller (create)
public function actionCreate()
$this->layout = '//layouts/column1';
// Uncomment the following line if AJAX validation is needed
// I better do a check in case no keywords were selected
$keywordArray = (!isset($_POST['keywordArray']) ? array() : $_POST['keywordArray']);
// now I either have an array of keywords or an empty array
// send the array to a function to format the keywords in a
// comma delimited format
// it's all done but not yet validated
Here is the function that formats the keywords
Remember the keywords are suppled by the admin so I am not doing any checking here for capitals or illegal characters.
public function setKeywords($model,$field,$keys)
$model->$field = (empty($keys) ? null : implode(',',$keys));
And when we perform an update on the record we have to turn our comma delimited field back into an array, so similarly.
public function getKeywords($delimited)
$keys = explode(',',$delimited);
And now for the rules (in the model file)
public function rules()
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
// other rules are here
// keywords is the field in the table currently holding the submitted value
// keywordCount is the function (in the model) that will do the validation
//skipOnError, maxKeys and minKeys are all parameters that will be
sent to the validation function
The validation function
public function keywordCount($attribute,$params)
$keys = explode(',',$this->$attribute);
// check if there are keywords exist
$this->addError($attribute,'<strong>If you need to add a keyword please contact the administrator</strong>');
// check if there are enough keywords
if(count($keys) < $params['minKeys'])
$this->addError($attribute,'Please choose at least '.$params['minKeys'].($params['minKeys'] > 1 ? ' keywords' :' keyword'));
// check if there are too many keywords
if(count($keys) > $params['maxKeys'])
$this->addError($attribute,'You have chosen too many keywords max = '.$params['maxKeys']);
Now the user can select between 2 and 6 keywords.