Jump to content

Añadir campo a formulario "create-account_form"


Recommended Posts

Tengo un problema al pasar un nuevo campo del formulario create-account_form. Cuando lo obtengo en processSubmitCreate() siempre esta vacio. La función getValue('ciudad_create') no devuelve nada.

 

He añadido el código en authentication.tpl para que se muestre el campo  CIudad

 

<label for="email_create">{l s='Email address'}</label>
                        <input type="email" class="is_required validate account_input form-control" data-validate="isEmail" id="email_create" name="email_create" value="{if isset($smarty.post.email_create)}{$smarty.post.email_create|stripslashes}{/if}" />

                        <label for="ciudad_create">{l s='City'}</label>
                        <input type="text" class="is_required validate account_input form-control" data-validate="isCity" id="ciudad_create" name="ciudad_create" value="{if isset($smarty.post.ciudad_create)}{$smarty.post.ciudad_create|stripslashes}{/if}" />
 

 

En la funcion de processSubmitCreate() de AuthController.php he añadido la comprobacion  (He añadido el campo ciudad a la clase Customer $customer->ciudad)

 

 

protected function processSubmitCreate()
    {
    if (!Validate::isCity($ciudad = trim(Tools::getValue('ciudad_create'))) || empty($ciudad)) {
            $this->errors[] = Tools::displayError('La ciudad introducice no es correcta: Barcelona, Madrid o Alicante');
    
        }
        elseif (!Validate::isEmail($email = trim(Tools::getValue('email_create'))) || empty($email)) {
            $this->errors[] = Tools::displayError('Invalid email address.');
    
        } elseif (Customer::customerExists($email)) {
            $this->errors[] = Tools::displayError('An account using this email address has already been registered. Please enter a valid password or request a new one. ', false);
            $_POST['email'] = trim(Tools::getValue('email_create'));
            unset($_POST['email_create']);
        } else {
            $this->create_account = true;
            $this->context->smarty->assign('email_create', Tools::safeOutput($email));
        $_POST['email'] = $email;           
        }
    }
 

 

La funcion isCity la he añadido en la clase Validate.php

 

  /**
     * Check for validity
     *
     * @param string $ciudad ciudad to validate
     * @return bool Validity is ok or not
     */

    public static function isCity($ciudad){
        $ciudades = array('Barcelona','Madrid','Alicante');  
        if (in_array($ciudad, $ciudades)){
                   return false;
        }else
            return true;
    }

Edited by [email protected] (see edit history)
Link to comment
Share on other sites

He metido esto en la función processSubmitCreate:

foreach ($_POST as $key => $value)

            $this->errors[] = Tools::displayError("Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>");
 
y esto es lo que obtengo: 
 
 
  1. Field controller is authentication<br>
  2. Field SubmitCreate is 1<br>
  3. Field ajax is true<br>
  4. Field email_create is [email protected]<br>
  5. Field back is my-account<br>
  6. Field token is 4f834c5cc5cf42cc2d28976faf373cc8<br>
Link to comment
Share on other sites

En primer lugar, muchas gracias por tus respuestas. La verdad es que creo que aqui puede estar el problema pero no lo tengo muy claro porque ya he probado tantas cosas...

 

En la funcion processSubmitCreate() tengo esto (En el primer comentario no esta) de AuthController :

 

else {
            $this->create_account = true;
            $this->context->smarty->assign('email_create', Tools::safeOutput($email));
            $this->context->smarty->assign('ciudad_create', $ciudad);
            $_POST['email'] = $email;
            $_POST['ciudad'] = $ciudad;
        }

 

pero claro en ese else entra despues de validar isCity y en ese momento necesito ya el getValue('ciudad') por lo que entiendo que igual no lo estoy poniendo donde corresponde.

Link to comment
Share on other sites

Buenas, la asignación de smarty la tienes que hacer cuando sacas el tpl, no puedes intentar hacer post de un campo no cargado. En primer lugar debes asignar a smarty las variables con su tpl + css correspondiente, después, en la llamada/hook que recoge el evento, recoges el submit. Saludos

Link to comment
Share on other sites

Bueno, creo que por el momento lo dejaré porque debo estar cometiendo algun error que ya no soy capaz de ver.

 

Lo que me comentabas imagino que era en authcontroller poner dentro de updateContext  $this->context->cookie->ciudad = $customer->ciudad;

 

Lo que no llego a entender es que en el siguiente formulario si que lo recogo bien y todo me funciona. Es al añadir el campo junto al e-mail en el primer proceso de creación.

 

Estoy perdida la verdad.

Edited by [email protected] (see edit history)
Link to comment
Share on other sites

La clase customer esta modificada para tener $customer->ciudad y el campo esta añadido en la base de datos. 

 

/** @var string ciudad */

    public $ciudad;
 
'ciudad' => array('type' => self::TYPE_STRING, 'required' => true, 'size' => 32),
 
De hecho en el formulario siguiente me funciona bien. y me llega a guardar en la base de datos la ciudad que le introduzco. 
Link to comment
Share on other sites

Buenas, revisa la asignación a smarty, ejemplo:

$this->smarty->assign(
				array(
					'products' => HomeFeatured::$cache_products,
					'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
					'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
				)
			);

Saludos

Link to comment
Share on other sites

Nada! Creo que me rindo! Esta claro que no hago la asignacion correctamente pero ya no se donde probar. 

 

La debería hacer en el processSubmitCreate pero no me funciona. He probado muchas opciones, la ultima:

 

$this->context->smarty->assign('ciudad_create', Tools::safeOutput($ciudad)); y $this->context->smarty->assign('ciudad_create', $ciudad);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...