Для добавления формы на страницу предназначен класс yii\widgets\ActiveForm. Кроме того, имеются дополнительные методы в yii\helpers\Html. Форма в большинстве случаев связана с моделью, которая проверяет данные формы на сервере. Модель наследует класс yii\db\ActiveRecord или класс yii\base\Model.
Создаем модель для формы обратной связи
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php /* * Файл models/FeedbackForm.php */ namespace app\models; use Yii; use yii\base\Model; /** * Модель для формы обратной связи */ class FeedbackForm extends Model { public $name ; public $email ; public $body ; } |
Создаем action для страницы, где будет форма обратной связи
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php /* * Файл controllers/PageController */ namespace app\controllers; use yii\web\Controller; use app\models\FeedbackForm; class PageController extends Controller { public function actionIndex() { return $this ->render( 'index' ); } public function actionFeedback() { $model = new FeedbackForm(); return $this ->render( 'feedback' , [ 'model' => $model ]); } } |
Создаем шаблон страницы формы обратной связи
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php /* @var $this yii\web\View */ use yii\widgets\ActiveForm; // стилизованная версия под bootstrap 3 // use yii\bootstrap\ActiveForm use yii\helpers\Html; $this ->title = 'Обратная связь' ; ?> <div class = "site-feedback" > <h1><?= Html::encode( $this ->title) ?></h1> <?php $form = ActiveForm::begin(); ?> <?= $form ->field( $model , 'name' )->textInput()->label( 'Имя, Фамилия' ); ?> <?= $form ->field( $model , 'email' )->input( 'email' )->label( 'Адрес почты' ); ?> <?= $form ->field( $model , 'body' )->textarea([ 'rows' => 5])->label( 'Ваше сообщение' ); ?> <?= Html::submitButton( 'Отправить' , [ 'class' => 'btn btn-primary' ]); ?> <?php ActiveForm:: end (); ?> </div> |
В результате получим такой html-код:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <form id= "w0" action= "/web/index.php?r=page%2Ffeedback" method= "post" > <input type= "hidden" name= "_csrf" value= "....." > <div class = "form-group field-feedbackform-name required" > <label class = "control-label" for = "feedbackform-name" >Имя, Фамилия</label> <input type= "text" id= "feedbackform-name" class = "form-control" name= "FeedbackForm[name]" aria-required= "true" > <div class = "help-block" ></div> </div> <div class = "form-group field-feedbackform-email required" > <label class = "control-label" for = "feedbackform-email" >Адрес почты</label> <input type= "email" id= "feedbackform-email" class = "form-control" name= "FeedbackForm[email]" aria-required= "true" > <div class = "help-block" ></div> </div> <div class = "form-group field-feedbackform-body required" > <label class = "control-label" for = "feedbackform-body" >Ваше сообщение</label> <textarea id= "feedbackform-body" class = "form-control" name= "FeedbackForm[body]" rows= "10" aria-required= "true" ></textarea> <div class = "help-block" ></div> </div> <button type= "submit" class = "btn btn-primary" >Отправить</button> </form> |
Можно добавить для формы идентификатор и класс, а для полей формы — подсказки и placeholder-ы:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <div class = "page-feedback" > <h1><?= Html::encode( $this ->title) ?></h1> <?php $form = ActiveForm::begin([ 'options' => [ 'id' => 'feedback-form' , 'class' => 'form-horizontal' ] ]); ?> <?= $form ->field( $model , 'name' ) ->textInput([ 'placeholder' => 'Ваше имя и фамилия' ]) ->hint( 'Ваше имя и фамилия' ) ->label( 'Имя, Фамилия' ); ?> <?= $form ->field( $model , 'email' ) ->input( 'email' , [ 'placeholder' => 'Ваш адрес почты' ]) ->hint( 'Ваш адрес почты' ) ->label( 'Адрес почты' ); ?> <?= $form ->field( $model , 'body' ) ->textarea([ 'rows' => 5, 'placeholder' => 'Введите ваше сообщение' ]) ->hint( 'Введите ваше сообщение' ) ->label( 'Ваше сообщение' ); ?> <?= Html::submitButton( 'Отправить' , [ 'class' => 'btn btn-primary' ]); ?> <?php ActiveForm:: end (); ?> </div> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <form id= "feedback-form" class = "form-horizontal" action= "/web/index.php?r=page%2Ffeedback" method= "post" > <input type= "hidden" name= "_csrf" value= "....." > <div class = "form-group field-feedbackform-name required" > <label class = "control-label" for = "feedbackform-name" >Имя, Фамилия</label> <input type= "text" id= "feedbackform-name" class = "form-control" name= "FeedbackForm[name]" placeholder= "Ваше имя и фамилия" aria-required= "true" > <div class = "hint-block" >Ваше имя и фамилия</div> <div class = "help-block" ></div> </div> <div class = "form-group field-feedbackform-email required" > <label class = "control-label" for = "feedbackform-email" >Адрес почты</label> <input type= "email" id= "feedbackform-email" class = "form-control" name= "FeedbackForm[email]" placeholder= "Ваш адрес почты" aria-required= "true" > <div class = "hint-block" >Ваш адрес почты</div> <div class = "help-block" ></div> </div> <div class = "form-group field-feedbackform-body required" > <label class = "control-label" for = "feedbackform-body" >Ваше сообщение</label> <textarea id= "feedbackform-body" class = "form-control" name= "FeedbackForm[body]" rows= "5" placeholder= "Введите ваше сообщение" aria-required= "true" ></textarea> <div class = "hint-block" >Введите ваше сообщение</div> |