ActiveForm.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/15
  6. * Time: 下午11:00
  7. */
  8. namespace backend\widgets;
  9. use yii\helpers\ArrayHelper;
  10. use yii\base\Model;
  11. use yii\helpers\Url;
  12. class ActiveForm extends \yii\widgets\ActiveForm
  13. {
  14. public $fieldClass = 'backend\widgets\ActiveField';
  15. public $boxFieldClass = '\backend\widgets\BoxField';
  16. public function init()
  17. {
  18. parent::init();
  19. if (!isset($this->validationUrl)) {
  20. if (!empty($this->action)) {
  21. $this->validationUrl = ArrayHelper::merge((array)$this->action, ['ajax-validate' => 1]);
  22. } else {
  23. $this->validationUrl = Url::current(['ajax-validate' => 1]);
  24. }
  25. }
  26. }
  27. /**
  28. * 可折叠
  29. * @param $model
  30. * @param $attribute
  31. * @param array $options
  32. * @return object
  33. */
  34. public function boxField($model, $attribute, $options = [])
  35. {
  36. $config = $this->fieldConfig;
  37. if ($config instanceof \Closure) {
  38. $config = call_user_func($config, $model, $attribute);
  39. }
  40. if (!isset($config['class'])) {
  41. $config['class'] = $this->boxFieldClass;
  42. }
  43. return \Yii::createObject(ArrayHelper::merge($config, $options, [
  44. 'model' => $model,
  45. 'attribute' => $attribute,
  46. 'form' => $this,
  47. ]));
  48. }
  49. /**
  50. * Generates a form field.
  51. * A form field is associated with a model and an attribute. It contains a label, an input and an error message
  52. * and use them to interact with end users to collect their inputs for the attribute.
  53. * @param Model $model the data model.
  54. * @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format
  55. * about attribute expression.
  56. * @param array $options the additional configurations for the field object. These are properties of [[ActiveField]]
  57. * or a subclass, depending on the value of [[fieldClass]].
  58. * @return ActiveField the created ActiveField object.
  59. * @see fieldConfig
  60. */
  61. public function field($model, $attribute, $options = [])
  62. {
  63. return parent::field($model, $attribute, $options);
  64. }
  65. }