Yii Framework Forum: Insertar varios emails en un campo - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Insertar varios emails en un campo cómo validar? Rate Topic: -----

#1 User is offline   safitanet 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 04-August 11

Posted 22 September 2011 - 09:01 AM

Hola, quisiera poner en las rules de un model que un campo acepta una dirección de email o más, separadas por coma, y que se compruebe que son válidas al ser introducidas desde el formulario.

Gracias y un saludo
0

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 22 September 2011 - 11:29 AM

Tendras que crear un 'custom' validator

// en rules
 array('emails', 'validateEmails'),

//....

function validateEmails($attribute, $params){
    if($this->hasErrors($this->{$attribute} || empty($this->{$attribute}) 
           return; // you do whatever

    // aqui tu funcion para validar los emails 
    // mira CEmailValidator para ideas de como validar
    // si no pasa la validacion: $this->addError('attributename','error');
}


Cheers
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#3 User is offline   safitanet 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 04-August 11

Posted 22 September 2011 - 11:35 AM

Muchas gracias Billy el rápido
0

#4 User is offline   safitanet 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 04-August 11

Posted 22 September 2011 - 12:25 PM

He hecho así el validate, según he entendido tu explicación y parece que funciona, si lo puedo mejorar agradezco cualquier sugerencia:

function validateEmails($attribute, $params){
if($this->hasErrors($this->{$attribute}) || empty($this->{$attribute}))
return $this->addError($attribute,'lista de emails incorrecta'); // you do whatever

$pattern='/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';

$emails= explode (',',$this->{$attribute});
for ($i=0;$i<count($emails);$i++)
{
$valid=is_string(trim($emails[$i])) && strlen(trim($emails[$i]))<=254 && (preg_match($pattern,trim($emails[$i])));
if (!$valid)
break;
}

if(!$valid)
return $this->addError($attribute,'lista de emails incorrecta'); // you do whatever

return TRUE;
}
0

#5 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 24 September 2011 - 03:25 PM

Yo modificaría la función ligeramente :) - solo por fastidiar

function validateEmails($attribute, $params){
    if($this->hasErrors($this->{$attribute}))
       return;
    if (empty($this->{$attribute})) // no permitido si esta vacio
       return $this->addError($attribute,'lista de emails no puede estar vacia'); 

    $pattern='/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';

     $emails= explode (',',$this->{$attribute}); // solo comas? y puntos y comas no? :)

     foreach($emails as $email)
     {
        if(!(is_string(trim($emails[$i])) && strlen(trim($emails[$i]))<=254 && (preg_match($pattern,trim($emails[$i]))))
           return $this->addError($attribute,'lista de emails incorrecta');
     }

}


Totalmente no 'testada'

Un saludo...

PS: Billi el rápido? :)
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users