|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Idma\LaravelParsley; |
| 4 | + |
| 5 | +use Illuminate\Html\FormBuilder as BaseFormBuilder; |
| 6 | + |
| 7 | +class FormBuilder extends BaseFormBuilder { |
| 8 | + /** |
| 9 | + * @type \Illuminate\Database\Eloquent\Model |
| 10 | + */ |
| 11 | + protected $model; |
| 12 | + protected $modelName = null; |
| 13 | + |
| 14 | + /** |
| 15 | + * @type Parsley |
| 16 | + */ |
| 17 | + protected $parsley = null; |
| 18 | + protected $validationRules = []; |
| 19 | + protected $customAttributes = []; |
| 20 | + |
| 21 | + public static $abc = 1; |
| 22 | + |
| 23 | + /** |
| 24 | + * {@inheritdoc} |
| 25 | + */ |
| 26 | + public function open(array $options = []) { |
| 27 | + if ($this->model && !isset($options['method'])) { |
| 28 | + $options['method'] = $this->model->getAttribute('id') ? 'put' : 'post'; |
| 29 | + } |
| 30 | + |
| 31 | + $method = strtoupper(array_get($options, 'method', 'post')); |
| 32 | + |
| 33 | + // We need to extract the proper method from the attributes. If the method is |
| 34 | + // something other than GET or POST we'll use POST since we will spoof the |
| 35 | + // actual method since forms don't support the reserved methods in HTML. |
| 36 | + $attributes['method'] = $this->getMethod($method); |
| 37 | + |
| 38 | + $attributes['action'] = $this->getAction($options); |
| 39 | + |
| 40 | + $attributes['accept-charset'] = 'UTF-8'; |
| 41 | + |
| 42 | + // If the method is PUT, PATCH or DELETE we will need to add a spoofer hidden |
| 43 | + // field that will instruct the Symfony request to pretend the method is a |
| 44 | + // different method than it actually is, for convenience from the forms. |
| 45 | + $append = $this->getAppendage($method); |
| 46 | + |
| 47 | + if (isset($options['files']) && $options['files']) { |
| 48 | + $options['enctype'] = 'multipart/form-data'; |
| 49 | + } |
| 50 | + |
| 51 | + // Finally we're ready to create the final form HTML field. We will attribute |
| 52 | + // format the array of attributes. We will also add on the appendage which |
| 53 | + // is used to spoof requests for this PUT, PATCH, etc. methods on forms. |
| 54 | + $attributes = array_merge($attributes, array_except($options, $this->reserved)); |
| 55 | + |
| 56 | + // Finally, we will concatenate all of the attributes into a single string so |
| 57 | + // we can build out the final form open statement. We'll also append on an |
| 58 | + // extra value for the hidden _method field if it's needed for the form. |
| 59 | + $attributes = $this->html->attributes($attributes); |
| 60 | + |
| 61 | + return '<form'.$attributes.'>'.$append; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * {@inheritdoc} |
| 66 | + */ |
| 67 | + public function model($model, array $options = []) { |
| 68 | + $this->setModel($model); |
| 69 | + |
| 70 | + return $this->open($options); |
| 71 | + } |
| 72 | + |
| 73 | + public function openModel($model, array $options = []) { |
| 74 | + return $this->model($model, $options); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritdoc} |
| 79 | + */ |
| 80 | + public function label($name, $value = null, $options = []) { |
| 81 | + $this->labels[] = $name; |
| 82 | + |
| 83 | + $options = $this->html->attributes($options); |
| 84 | + |
| 85 | + $value = e($this->formatLabel($name, $value)); |
| 86 | + |
| 87 | + $for = ($this->modelName && !starts_with($name, '_')) ? $this->modelName.'-'.$name : $name; |
| 88 | + |
| 89 | + return '<label for="'.$for.'"'.$options.'>'.$value.'</label>'; |
| 90 | + } |
| 91 | + |
| 92 | + public function helpBlock($value, array $options = []) { |
| 93 | + if (isset($options['class'])) { |
| 94 | + $options['class'] = 'help-block '.$options['class']; |
| 95 | + } else { |
| 96 | + $options['class'] = 'help-block'; |
| 97 | + } |
| 98 | + |
| 99 | + return '<span'.$this->html->attributes($options).'>'.$value.'</span>'; |
| 100 | + } |
| 101 | + |
| 102 | + public function input($type, $name, $value = null, $options = []) |
| 103 | + { |
| 104 | + $options = array_merge($options, $this->createParsleyRulesForField($name)); |
| 105 | + return parent::input($type, $name, $value, $options); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param $name |
| 110 | + * |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + protected function createParsleyRulesForField($name) { |
| 114 | + if (isset($this->validationRules[$name]) && !starts_with($name, '_')) { |
| 115 | + $rules = explode('|', $this->validationRules[$name]); |
| 116 | + $parsleyRules = []; |
| 117 | + |
| 118 | + $parsleyRules = array_merge($parsleyRules, $this->parsley->convertRules($name, $rules, $this->customAttributes)); |
| 119 | + |
| 120 | + return $parsleyRules; |
| 121 | + } |
| 122 | + |
| 123 | + return []; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * {@inheritdoc} |
| 128 | + */ |
| 129 | +// public function getIdAttribute($name, $attributes) { |
| 130 | +// $id = null; |
| 131 | +// |
| 132 | +// if (array_key_exists('id', $attributes)) { |
| 133 | +// $id = $attributes['id']; |
| 134 | +// } else { |
| 135 | +// $id = $name; |
| 136 | +// } |
| 137 | +// |
| 138 | +// if ($this->modelName && $id && !starts_with($id, '_')) { |
| 139 | +// return $this->modelName.'-'.$id; |
| 140 | +// } |
| 141 | +// |
| 142 | +// return null; |
| 143 | +// } |
| 144 | + |
| 145 | + public function setModel($model) { |
| 146 | + $this->model = $model; |
| 147 | + $this->modelName = strtolower((new \ReflectionClass($this->model))->getShortName()); |
| 148 | + |
| 149 | + if (method_exists($this->model, 'getParsleyRules')) { |
| 150 | + $this->validationRules = $this->model->getParsleyRules(); |
| 151 | + |
| 152 | + if (property_exists($this->model, 'getParsleyCustomAttributes')) { |
| 153 | + $_model = $this->model; |
| 154 | + $this->customAttributes = $_model::$customAttributes; |
| 155 | + } |
| 156 | + |
| 157 | + $this->parsley = new Parsley($this->customAttributes); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Gets the short model name. |
| 163 | + * |
| 164 | + * @return string |
| 165 | + */ |
| 166 | + public function getModelName() { |
| 167 | + return $this->modelName; |
| 168 | + } |
| 169 | + |
| 170 | + public function name() { |
| 171 | + return $this->getModelName(); |
| 172 | + } |
| 173 | +} |
0 commit comments