Advertisement
slo_nik

calculateForm

Oct 29th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\modules\proposal\models\frontend;
  4.  
  5. use app\modules\routes\models\Cities;
  6. use app\modules\routes\models\RoutesCities;
  7. use Yii;
  8.  
  9. use app\modules\routes\models\Tariffs;
  10. use app\modules\proposal\models\Proposal;
  11. use yii\helpers\ArrayHelper;
  12.  
  13. class CalculateForm extends Proposal
  14. {
  15.  
  16.     public $cargoData;
  17.     public $mass;
  18.     public $capacity;
  19.     public $cargoGeometry;
  20.     public $pattern;
  21.     public $msg;
  22.     public $prePayPrice;
  23.     public $postPayPrice;
  24.  
  25.     const SCENARIO_CALC_FORM = 'CalculateForm';
  26.     const SCENARIO_POPUP_FORM = 'CalculatePopupForm';
  27.  
  28.     public function init()
  29.     {
  30.         parent::init();
  31.         $this->pattern = '^[ 0-9.,]+$';
  32.     }
  33.  
  34.     public function rules()
  35.     {
  36.         return ArrayHelper::merge(parent::rules(),[
  37.  
  38.             [['from', 'to'], 'required', 'on' => self::SCENARIO_CALC_FORM],
  39.  
  40.             ['to', 'compare', 'compareAttribute' => 'from', 'operator' => '!=', 'on' => self::SCENARIO_CALC_FORM],
  41.             ['from', 'compare', 'compareAttribute' => 'to', 'operator' => '!=', 'on' => self::SCENARIO_CALC_FORM],
  42.             [
  43.                 'message', 'match', 'pattern' => '#^[-а-яёa-z0-9  ,."]+$#iu',
  44.                 'message' => '«{attribute}» может содержать А-ЯЁ, A-Z, тире и цифры', 'on' => self::SCENARIO_CALC_FORM],
  45.             [
  46.                 'name', 'match', 'pattern' => '#^[а-яё ]+$#iu',
  47.                 'message' => '«{attribute}» может содержать А-ЯЁ', 'on' => self::SCENARIO_POPUP_FORM
  48.             ],
  49.             [
  50.                 'phone', 'match', 'pattern' => '#\+7 \(\d{3}\) \d{3}-\d{2}-\d{2}#',
  51.                 'message' => '«{attribute}» должен быть в формате +7(222) 333-44-55', 'on' => self::SCENARIO_POPUP_FORM
  52.             ],
  53.             [
  54.                 'phone', 'required', 'when' => function($model){
  55.                      return $model->email == '';
  56.                  },
  57.                 'on' => self::SCENARIO_POPUP_FORM
  58.             ],
  59.             [
  60.                 'email', 'required', 'when' => function($model){
  61.                      return $model->phone == '';
  62.                 },
  63.                 'on' => self::SCENARIO_POPUP_FORM
  64.             ],
  65.             [['message', 'name', 'cargoData'], 'string', 'on' => self::SCENARIO_CALC_FORM],
  66.  
  67.             ['email', 'email', 'on' => self::SCENARIO_CALC_FORM],
  68.  
  69.             [['mass', 'capacity'], 'number', 'on' => self::SCENARIO_CALC_FORM],
  70.  
  71.             ['capacity', 'default', 'value' => '0.005', 'on' => self::SCENARIO_CALC_FORM],
  72.  
  73.             ['weight', 'double'],
  74.  
  75.  
  76.         ]);
  77.     }
  78.  
  79.     public function scenarios()
  80.     {
  81.         $scenarios = parent::scenarios();
  82.         $scenarios[self::SCENARIO_CALC_FORM] = ['from', 'to', 'message', 'name', 'email', 'phone', 'mass', 'capacity', 'cargoData'];
  83.         return $scenarios;
  84.     }
  85.  
  86.     public function attributeLabels()
  87.     {
  88.         return ArrayHelper::merge(parent::attributeLabels(), [
  89.             'mass' => 'Massa',
  90.             'capacity' => 'Capacity'
  91.         ]);
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement