Esempio n. 1
1
 /**
  * Add error class to wrapper if validation errors exist
  *
  * @param $options
  */
 protected function addErrorClass(&$options)
 {
     $errors = $this->parent->getRequest()->session()->get('errors');
     if ($errors && $errors->has($this->getNameKey())) {
         $errorClass = $this->formHelper->getConfig('defaults.wrapper_error_class');
         if ($options['wrapper'] && !str_contains($options['wrapper']['class'], $errorClass)) {
             $options['wrapper']['class'] .= ' ' . $errorClass;
         }
     }
     return $options;
 }
 /**
  * @return Form
  * @throws \Exception
  */
 protected function getClassFromOptions()
 {
     if ($this->form instanceof Form) {
         return $this->form->setName($this->name);
     }
     $class = $this->getOption('class');
     if (!$class) {
         throw new \InvalidArgumentException('Please provide full name or instance of Form class.');
     }
     if (is_string($class)) {
         $formOptions = array_merge(['model' => $this->parent->getModel(), 'name' => $this->name], $this->getOption('formOptions'));
         $data = array_merge($this->parent->getData(), $this->getOption('data'));
         return $this->parent->getFormBuilder()->create($class, $formOptions, $data);
     }
     if ($class instanceof Form) {
         if (!$class->getModel()) {
             $class->setModel($this->parent->getModel());
         }
         if (!$class->getData()) {
             $class->addData($this->parent->getData());
         }
         return $class->setName($this->name);
     }
     throw new \InvalidArgumentException('Class provided does not exist or it passed in wrong format.');
 }
Esempio n. 3
0
 public static function buildForm(Form $form, $config)
 {
     if (!$config) {
         $form->add('post_id', 'entity', ['label' => 'Post', 'class' => 'App\\Post', 'property' => 'title', 'empty_value' => 'Please select a post', 'rules' => 'required', 'required' => true]);
     }
     $form->add('created_at', 'date', ['label' => 'Date', 'rules' => 'required|date', 'required' => true, 'default_value' => Carbon::now()->format('y-m-d')])->add('name', 'text', ['label' => 'User name', 'rules' => 'required|min:3', 'required' => true])->add('email', 'text', ['label' => 'User e-mail', 'rules' => 'required|email', 'required' => true])->add('body', 'textarea', ['label' => 'Text', 'rules' => 'required|max:5000|min:5', 'required' => true]);
 }
Esempio n. 4
0
 /**
  * @param Form $form
  */
 protected function determineLocaleField(Form $form)
 {
     if (locale_count() > 1) {
         $locales = [];
         foreach (locales() as $locale) {
             $locales[$locale] = trans('general.' . $locale);
         }
         $form->addAfter('type', 'locale', 'select', ['inline' => true, 'choices' => $locales, 'selected' => env('REACTOR_LOCALE')]);
     }
 }
Esempio n. 5
0
 /**
  * Prepare options for rendering
  *
  * @param array $options
  * @return array
  */
 protected function prepareOptions(array $options = [])
 {
     $helper = $this->formHelper;
     if (array_get($this->options, 'template') !== null) {
         $this->template = array_pull($this->options, 'template');
     }
     $options = $helper->mergeOptions($this->options, $options);
     if ($this->parent->haveErrorsEnabled()) {
         $this->addErrorClass($options);
     }
     if ($this->getOption('attr.multiple')) {
         $this->name = $this->name . '[]';
     }
     if ($this->getOption('required') === true) {
         $options['label_attr']['class'] .= ' ' . $this->formHelper->getConfig('defaults.required_class', 'required');
         $options['attr']['required'] = 'required';
     }
     $options['wrapperAttrs'] = $helper->prepareAttributes($options['wrapper']);
     $options['errorAttrs'] = $helper->prepareAttributes($options['errors']);
     if ($options['is_child']) {
         $options['labelAttrs'] = $helper->prepareAttributes($options['label_attr']);
     }
     if ($options['help_block']['text']) {
         $options['help_block']['helpBlockAttrs'] = $helper->prepareAttributes($options['help_block']['attr']);
     }
     return $options;
 }
 /**
  * @inheritdoc
  */
 public function removeChild($key)
 {
     if ($this->getChild($key)) {
         $this->form->remove($key);
         return parent::removeChild($key);
     }
     return $this;
 }
Esempio n. 7
0
 /**
  * Validate filter
  *
  * @return boolean
  */
 protected function validateFilter()
 {
     if (is_null($this->form_filter)) {
         return false;
     }
     $filterValidation = $this->getFilterValidation();
     $messages = $filterValidation['messages'];
     $this->form_filter->validate([], $messages);
     return $this->form_filter->isValid();
 }
Esempio n. 8
0
 /**
  * Add error class to wrapper if validation errors exist
  */
 protected function addErrorClass()
 {
     $errors = $this->parent->getRequest()->session()->get('errors');
     if ($errors && $errors->has($this->getNameKey())) {
         $errorClass = $this->formHelper->getConfig('defaults.wrapper_error_class');
         $wrapperClass = $this->getOption('wrapper.class');
         if ($this->getOption('wrapper') && !str_contains($wrapperClass, $errorClass)) {
             $wrapperClass .= ' ' . $errorClass;
             $this->setOption('wrapper.class', $wrapperClass);
         }
     }
 }
Esempio n. 9
0
 protected function setupLabel()
 {
     if ($this->getOption('label') !== null) {
         return;
     }
     if ($langName = $this->parent->getLanguageName()) {
         $label = sprintf('%s.%s', $langName, $this->getRealName());
     } else {
         $label = $this->getRealName();
     }
     $this->setOption('label', $this->formHelper->formatLabel($label));
 }
Esempio n. 10
0
 /**
  * Update Form data
  *
  * @param $data
  */
 protected function setFormData($data)
 {
     foreach ($data as $key => $value) {
         if ($key == "i18n") {
             foreach ($value as $locale => $fields) {
                 foreach ($fields as $field => $content) {
                     $key = "i18n][{$locale}][{$field}";
                     if (!$this->form->has($key)) {
                         continue;
                     }
                     $oldField = $this->form->getField($key);
                     $this->form->modify($key, $oldField->getType(), ['attr' => ['data-previous' => $value[$locale][$field]], 'value' => $value[$locale][$field]]);
                 }
             }
         } else {
             if (!$this->form->has($key)) {
                 continue;
             }
             $oldField = $this->form->getField($key);
             $this->form->modify($key, $oldField->getType(), ['attr' => ['data-previous' => is_array($value) ? json_encode($value) : $value], 'value' => $value]);
         }
     }
 }
Esempio n. 11
0
 public static function buildForm(Form $form, $config)
 {
     $form->add('text', 'text', ['label' => 'Tag text', 'rules' => 'required', 'required' => true])->add('posts', 'lookup', ['model' => 'App\\Post', 'property' => 'title', 'label' => 'Posts']);
 }
Esempio n. 12
0
 /**
  * It's the Laravel-From-Builder hack to fix error borders on all fields with the same name.
  *
  * @param Form $form
  * @param string $errorClass
  */
 function fixFormBuilderForm(Form $form, $errorClass)
 {
     foreach ($form->getFields() as $field) {
         $class = str_replace($errorClass, '', $field->getOption('wrapper.class'));
         $field->setOption('wrapper.class', $class);
     }
 }
    protected function setupForm(Form $form)
    {
        $form->setFormHelper($this->formHelper)
            ->setFormBuilder($this->formBuilder);

        $form->buildForm();
        return $form;
    }
Esempio n. 14
0
 public static function buildForm(Form $form, $config)
 {
     $form->add('created_at', 'date', ['label' => 'Date', 'rules' => 'required|date', 'required' => true, 'default_value' => Carbon::now()->format('y-m-d')])->add('title', 'text', ['label' => 'Post title', 'rules' => 'required|min:5', 'required' => true])->add('body', 'textarea', ['label' => 'Post text', 'rules' => 'required|max:5000|min:5', 'required' => true])->add('image', 'image', ['label' => 'Image', 'rules' => 'required|lavanda_image:jpeg,gif,png', 'required' => true])->add('tags', 'lookup', ['model' => 'App\\Tag', 'property' => 'text', 'label' => 'Tags'])->add('comments', 'rowset', ['model' => 'App\\Comment', 'label' => 'Comments', 'row_label' => 'Comment']);
 }
Esempio n. 15
0
 function form_until(Form $form, $field_name)
 {
     return $form->renderUntil($field_name, false);
 }
Esempio n. 16
0
 /**
  * Set model to form object
  *
  * @param mixed $model
  * @return $this
  */
 public function setModel($model)
 {
     parent::setModel($model);
     $oldData = old();
     $this->model = $oldData ? $oldData : $model;
 }