pmtpenza

Untitled

Sep 6th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. namespace frontend\models;
  4.  
  5. use Yii;
  6. use yii\base\Model;
  7. use common\models\Contactmaster;
  8. use yii\base\Exception;
  9. use yii\helpers\ArrayHelper;
  10.  
  11. /**
  12. * ContactForm is the model behind the contact form.
  13. */
  14. class ContactForm extends Model
  15. {
  16. public $name;
  17. public $verifyCode;
  18. public $phone;
  19. public $specialnosty_id;
  20.  
  21.  
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function rules()
  26. {
  27. return [
  28. // name, email, subject and body are required
  29. [['name', 'phone'], 'required'],
  30. [['specialnosty_id'], 'integer'],
  31. [['name', 'phone'], 'string', 'max' => 50],
  32. // email has to be a valid email address
  33. // verifyCode needs to be entered correctly
  34. ['verifyCode', 'captcha'],
  35. ];
  36. }
  37.  
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'verifyCode' => 'Введите код с картинки',
  45. 'name' => 'Ваше имя',
  46. 'phone' => 'Ваш номер телефона',
  47. 'specialnosty_id' => 'Специальность',
  48. ];
  49. }
  50. public function getModel()
  51. {
  52. if (!$this->model) {
  53. $this->model = new Contactmaster();
  54. }
  55. return $this->model;
  56. }
  57. public function setModel($model)
  58. {
  59. $this->name = $model->name;
  60. $this->phone = $model->phone;
  61.  
  62. $this->model = $model;
  63.  
  64. return $this->model;
  65. }/**
  66. * Sends an email to the specified email address using the information collected by this model.
  67. *
  68. * @param string $email the target email address
  69. * @return bool whether the email was sent
  70. */
  71. public function save()
  72. {
  73. if ($this->validate()) {
  74. $model = $this->getModel();
  75. $isNewRecord = $model->getIsNewRecord();
  76. $model->name = $this->name;
  77. $model->phone = $this->phone;
  78.  
  79. if (!$model->save()) {
  80. throw new Exception('Model not saved');
  81. }
  82. }
  83. return $model;
  84. }
  85.  
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment