hi, i have multiple scenarios, e.g index, create
are we allowed to put e.g 2 scenarios in one validation rule?
e.g array(attributes,validation,‘on’=>‘index,create’), <-- is this correct ?
hi, i have multiple scenarios, e.g index, create
are we allowed to put e.g 2 scenarios in one validation rule?
e.g array(attributes,validation,‘on’=>‘index,create’), <-- is this correct ?
yes that’ s right : see the guide
let’s say the model is being used by different controllers
e.g mainmodel rules being used by
firstcontroller’s actionIndex()
secondcontroller’s actionCreate()
will the rules work even if the model’s rules is not just being used
by maincontroller but by another 2 controllers?
Model ’ s constructor has a optional parameter please look at it
i think you might misunderstand the ‘scenario’ it 's not the action id of some controller .
‘insert’,‘update’,‘search’ is just coincidence . you can call it any thing , if you know the “use case driven development” you will know what the ‘scenario’ actually mean .
and the model can used by any controller , the scenario do not matter to controller but just to the “use case”. gii may give you a illusion : “the User model can only used by UserCotnroller” .
the rules is just applied when massive assignment happen or save method called .
hey, i wonder what’s wrong with my function,
black list model code
public function getBlacklistWords()
{
$criteria = new CDbCriteria;
$criteria->select = 'blacklistwords';
$criteria->distinct = true;
$this->lists = blacklistwds::model()->findAll($criteria);
return $this->lists;
}
constant within the model where the rules reside
const TRAPPED = 1;
rules
return array(
array('ATTRIBUTE','blocker','trapped'=>self::TRAPPED),
);
validator within the model
public function blocker($attribute,$params)
{
$this->wordlist = blacklistwds::model()->getBlacklistWords();
foreach($this->wordlist as $obj){
array_push($this->words,$obj->blacklistwords);
}
if($params['trapped'] === self::TRAPPED)
{
if(in_array($this->attribute,$this->words))
{
$this->addError($attribute,"There's a blacklisted word in the form");
}
}
}
when i ran the page and included some blacklisted words inside, nothing happens …did i do something
wrong ?, i actually just followed the online manual from yii, on how to create own validation within the model
a simple way to check your works :
public function blocker($attribute,$params)
{
if($params['trapped'] === self::TRAPPED)
{
if(in_array($this->{$attribute},array('badWord1','badWord2'...)))
{
$this->addError($attribute,"There's a blacklisted word in the form");
}
}
}
just replace the $this->words to a static array , if the result is same to your expectation ,then construct the $this->words .
some times the error occurs is not the exactly place but by some other errors interfered
Hello
[s]If you have dumped your $params in your validation method, you’d see what is wrong
Hint: $params must be an array
Rules array :
return array(
array('ATTRIBUTE', 'blocker', array('trapped'=>self::TRAPPED)),
);
[/s]
I was wrong
to be frank i never used the params in inline validator . thanks for pointing out that .
thanks for the help …