public function actionValidcode()
{
$code = $_POST['Operator']['code'];
$code1 = Operator::model()->findAll('code=:vcode', array(
':vcode'=>$code,
));
foreach($code1 as $a)
{
echo $code.' sudah terdapat dalam database..!';
}
}
Why don’t leave the form as it was, enable Client+Ajax validation, and move your action to a function in the model, and add that function in the model rules for that field?
in the Operator model, taking your validation code:
class Operator extends CActiveRecord
{
(…)
public function rules()
{
return array(
(…)
array('code', 'validateCode'),
(…)
}
(…)
public function validateCode($attribute, $params) {
if(isset($_POST['Operator']['code'])) {
$code = $_POST['Operator']['code'];
$code1 = Operator::model()->findAll('code=:vcode', array(
':vcode'=>$code,
));
foreach($code1 as $a)
$this->addError($attribute, $code.' sudah terdapat dalam database..!');
}
return false;
}
(…)
}
On a side note, I don’t understand your error message, but it seems like you want $code to be unique? If that’s the case, you could directly specify it in the rules with your custom message.
array('code', 'unique', 'message'=>' sudah terdapat dalam database..!');
I don’t know whether it’s possible to display the value of code attribute in the error message.