I have model form (SomeForm) and custom validation function in there:
use yii\base\Model;
class SomeForm extends Model
{
public $age;
public function custom_validation($attribute, $params){
if($this->age < 18){
$this->addError($attribute, 'Some error Text');
return true;
}
else{
return false;
}
}
public function rules(){
return [
['age', 'custom_validation']
];
}
}
I use this custom_validation in rules() function but form even submitting whatever value has age attribute.
Here is the form:
age.php
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'age')->label("Age") ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
and the controller:
use yii\web\Controller;
class SomeController extends Controller
{
//this controller is just for rendering
public function actionIndex(){
return $this->render('age');
}
public function actionSubmit(){
$model = new SomeForm();
if($model->load(Yii::$app->request->post()){
//do something here
}
}
}
You don't need to return anything just adding the error to the attribute is enough.
Since version
2.0.11you can useyii\validators\InlineValidator::addError()for adding errors instead of using$this. That way the error message can be formatted usingyii\i18n\I18N::format()right away.Use
{attribute}and{value}in the error message to refer to an attribute label (no need to get it manually) and attribute value accordingly:What I suspect is the problem in your case is that you are missing the
$formModel->validate()as in the model given above extends theyii\base\Modeland not\yii\db\ActiveRecordand you must be saving some otherActiveRecordmodel and want to validate thisFormModelbefore saving theActiveRecordmodel, you have to call the$formModel->validate()to check if valid input is provided and trigger the model validation after loading the post array to the model.And another thing to notice is by default, inline validators will not be applied if their associated attributes receive empty inputs or if they have already failed some validation rules. If you want to make sure a rule is always applied, you may configure the
skipOnEmptyand/orskipOnErrorproperties to befalsein the rule declarations.Your model should look like below you are missing the
namespacein your model definition if that is not just intentional or due to sample code. just update you namespace according to the path where it is.your
controller/actionshould look likeThe Input field
agein the view file should use the$formMoedlobject