@darion
In Yii, you can use several built-in validators to validate form input. Here are the steps to validate input in Yii:
1 2 3 4 5 6 7 8 |
public function rules() { return [ [['name', 'email'], 'required'], ['email', 'email'], ['age', 'integer', 'min' => 18], ]; } |
1 2 3 4 5 6 7 |
$model = new User(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { // Input is valid, perform further actions } else { // Input is invalid, show form with error messages return $this->render('create', ['model' => $model]); } |
1 2 3 4 5 6 7 |
if ($model->hasErrors()) { foreach ($model->getErrors() as $attribute => $errors) { foreach ($errors as $error) { echo $model->getAttributeLabel($attribute) . ' ' . $error . ''; } } } |
With these steps, you can validate input in Yii using built-in validators. You can also create custom validators if needed.