Beispiel #1
0
 public function validateField(\Kodazzi\Form\Field $widget, $data)
 {
     $name_field = $widget->getName();
     $type = strtoupper(basename(str_replace('\\', '/', get_class($widget))));
     if ($widget->isRequired()) {
         // Si no existe el campo o esta vacio
         if (!array_key_exists($name_field, $data) || array_key_exists($name_field, $data) && $data[$name_field] === '') {
             // Si se esta editando y el campo es password, file o imagen se ignora ya que estos campos pueden estar vacios
             if (!$this->isNew() && in_array($type, array('PASSWORD', 'FILE', 'IMAGE'))) {
                 return;
             }
             $_error = strtr($this->I18n->get('form.required'), array('%name%' => $widget->getValueLabel()));
             $this->all_errors[$name_field] = $_error;
             $widget->setError($_error);
             $widget->setValid(false);
             $this->is_valid = false;
             return;
         }
     }
     if (array_key_exists($name_field, $data)) {
         // Si el campo no es requerido
         // Si el campo es un archivo o imagen
         // Si el campo esta vacio.
         // No se debe validar.
         if (in_array($type, array('FILE', 'IMAGE')) && ($data[$name_field] == '' || $data[$name_field] == null)) {
             return;
         } else {
             if ($data[$name_field] === '' || $data[$name_field] === null) {
                 $this->clean_data[$name_field] = '';
             } else {
                 if (!$widget->validate($data[$name_field])) {
                     /* Indica el objeto que tiene un error */
                     $widget->setValid(false);
                     $this->is_valid = false;
                     $this->all_errors[$name_field] = $widget->getError();
                 } else {
                     // Si hay archivos para subir guarda el widget en un array
                     if ($widget->hasUpload()) {
                         $this->files_uploads[$name_field] = $widget;
                         // Se almacena el nuevo nombre que tendra el archivo.
                         // No se utiliza getValue porque en los campos tipo FILE e IMAGE el value es una instancia de UploadedFile, componente de Symfony.
                         $this->clean_data[$name_field] = $widget->getNewName();
                     } else {
                         $this->clean_data[$name_field] = $widget->getValue();
                     }
                 }
             }
         }
     } else {
         if ($type == 'CHECK') {
             $this->clean_data[$name_field] = '';
         }
     }
 }
Beispiel #2
0
 public function setWidget($name, Field $widget)
 {
     $nameTranslation = null;
     $keyTranslation = $this->getNameForm() . '.' . $name;
     // Verifica si en el catalog existe una traduccion para el campo.
     if ($this->I18n->getCatalogue()->has($keyTranslation)) {
         $nameTranslation = $this->I18n->get($keyTranslation);
     }
     $widget->setName(strtolower($name));
     $widget->setValueLabel($nameTranslation);
     $widget->setNameForm($this->name_form);
     $widget->setForm($this);
     $widget->setI18n($this->I18n);
     $widget->setConfig($this->config_fields);
     $widget->setPathUpload($this->path_upload);
     $this->widgets[$name] = $widget;
     return $widget;
 }