Overwrite Zend Validation Message - zend-framework

How can I overwrite Zend validation messages in my code below.
$validatee = array(
'email' => $email,
);
$validator = array(
'email' => array(
'EmailAddress',
'messages' => array('emailAddressInvalidFormat',"Invalid Email Address")
)
);
$emailValidator = new Zend_Filter_Input(null, $validator,$validatee);
I tried doing that but the message doesn't change and always output
"no valid email address in the basic format local-part#hostname"
please help!

Change your $validator to ::
can you try adding
$validator = new Zend_Validate_EmailAddress();
$validator->setMessage(
'A valid email is required',
Zend_Validate_EmailAddress::INVALID
);
and then use in Zend_filter_input?

Related

How to send an email to the user when performing login in cakephp

I do not have any knowledge about cakephp mail so explain the solution briefly I mean what to do and how to do from the beginning.
From the cakephp official side I just used this "use Cake\Mailer\Email;" and then the mail function but a error message shows like as shown below
Could not send email: mail(): Failed to connect to mailserver at
quot;server.com" port 25, verify your "SMTP" and
quot;smtp_port" setting in php.ini or use ini_set()
MY users controller login function
public function login() {
$this->viewBuilder()->setLayout('');
if ($this->request->is('post')) {
$data = $this->request->getData();
$query = $this->Users->find()->where(['email' => $data['email'], 'password' => md5($data['password'])]);
if ($query->count()) {
$user = $query->first()->toArray();
$this->Auth->setUser($user);
//FOR MAIL START
ini_set('SMTP', "server.com");
ini_set('smtp_port', "25");
ini_set('sendmail_from', "restrange5#gmail.com");
$email = new Email('default');
$email->setFrom(['restrange5#gmail.com' => 'My Site'])
->setTo('ramakantasahoo835#gmail.com')
->setSubject('About')
->send('My message');
//FOR MAIL END
$this->Flash->success(_('Login Successfull'));
$this->redirect(['action' => 'dashboard']);
} else {
$this->Flash->error(__('Username/Password not found!!'));
return $this->redirect($this->referer());
}
}
}
How much I know as I have just changed in users controller only. What else I have do please suggest.
**Your cofiguration in app.php file is something like this **'
EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'email here',
'password' => 'password here',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
]

CakeEmail non-object error in server

I'm using CakePHP 2.0.6.
I'm trying to send a simple Email using CakeEmail.
In my controller I have:
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
in my controller's action:
$email = new CakeEmail('default');
$email->to('myemail#email.com')
->template('template')
->viewVars(array('var' => $this->Object->findById($this->Object->id)))
->emailFormat('html')
->subject('Subject')
->send();
My default email config is:
public $clkei = array(
'host' => 'ssl://smtpout.secureserver.net',
'port' => 465,
'username' => 'test#test.com',
'password' => 'password',
'from' => array('test#test.com' => 'My Name'),
'transport' => 'Smtp'
);
However I keep getting:
Fatal error: Call to a member function template() on a non-object in .../app/Controller/MyController.php on line 82
What I am missing?
Thank you!
have it tried it without 'viewVars' and 'template'? just for testing try
$email = new CakeEmail('default');
$email->to('myemail#email.com')
->template('template')
->viewVars(array('var' => $this->Object->findById($this->Object->id)))
->emailFormat('html')
->subject('Subject')
->send();

Zend Framework Default Error

How can i get rid of Zend default error not to show "Value is required and can't be empty"
Thanks
I'm using this method:
$this->addElement(
'text',
'testname',
array(
'label' => 'User Name',
'required' => true,
'filters' => array(
'StringTrim'
)
)
);
If you need to change the actual message text then use something like this:
$field = new Zend_Form_Element_Text('field');
$field ->setRequired(TRUE)
->addValidator(
'NotEmpty', //validator name
FALSE, //do not break on failure
array(messages' => array(
'isEmpty' => 'INSERT CUSTOM MESSAGE HERE'
)
)
)
Here you change the 'isEmpty' message of the NotEmpty validator.
Or if you defined the element differently.
$element = $this->getElement('text');
$element->addValidator('NotEmpty', //validator name
FALSE, //do not break on failure
array(messages' => array(
'isEmpty' => 'INSERT CUSTOM MESSAGE HERE'
)
)
)
In Zend_Form this is the default error message of the default validator 'Empty'.
To remove this validator, on the element add the removeValidator() function with 'empty' as the parameter:
$element->removeValidator('Empty');
This is assuming you have made a form in Zend_Form, that you have POSTed an empty value and are trying to validate it?
Going forward, please provide more information.

Custom Error Message for Captcha Element In Zend Framework 1.10

I am trying to set my own custom error message onto my Captcha but for some reason it is echoing twice.
Here is my captcha code:
$captcha = new Zend_Form_Element_Captcha(
'captcha', // This is the name of the input field
array('captcha' => array(
// First the type...
'captcha' => 'Image',
// Length of the word...
'wordLen' => 6,
// Captcha timeout, 5 mins
'timeout' => 300,
// What font to use...
'font' => 'images/captcha/font/arial.ttf',
// URL to the images
'imgUrl' => '/images/captcha',
//alt tag to keep SEO guys happy
'imgAlt' => "Captcha Image - Please verify you're human"
)));
And then to set my own error message:
$captcha->setErrorMessages(array('badCaptcha' => 'My message here'));
When the validation fails I get:
'My message here; My message here'
Why is it duplicating the error and how do I fix it?
After spending a LOT of time trying to get this to work, I've ended up setting the messages in the options of the constructor
$captcha = new Zend_Form_Element_Captcha(
'captcha', // This is the name of the input field
array(
'captcha' => array(
// First the type...
'captcha' => 'Image',
// Length of the word...
'wordLen' => 6,
// Captcha timeout, 5 mins
'timeout' => 300,
// What font to use...
'font' => 'images/captcha/font/arial.ttf',
// URL to the images
'imgUrl' => '/images/captcha',
//alt tag to keep SEO guys happy
'imgAlt' => "Captcha Image - Please verify you're human",
//error message
'messages' => array(
'badCaptcha' => 'You have entered an invalid value for the captcha'
)
)
)
);
I looked into this answer, but I didn't really like this solution, Now I have done it using an inputspecification like:
public function getInputSpecification()
{
$spec = parent::getInputSpecification();
if (isset($spec['validators']) && $spec['validators'][0] instanceof ReCaptcha) {
/** #var ReCaptcha $validator */
$validator = $spec['validators'][0];
$validator->setMessages(array(
ReCaptcha::MISSING_VALUE => 'Missing captcha fields',
ReCaptcha::ERR_CAPTCHA => 'Failed to validate captcha',
ReCaptcha::BAD_CAPTCHA => 'Failed to validate captcha', //this is my custom error message
));
}
return $spec;
}
I just noticed, this was a question for ZF1
This is the answer for ZF2

Problem with zend validate on zend form element

I used to have this form element to validate an email and display an error message if the format was invalid:
$email_users = new Zend_Form_Element_Text('email_users');
$email_users->setLabel('Email:')
->setRequired(false)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('EmailAddress')
->setErrorMessages(array('messages' => 'Invalid Email'));
setErrorMessages worked fine because this was the only validation I needed so it replaced all error messages with my custom one, now I had to add another validation to see if it already existed in my DB:
$email_users = new Zend_Form_Element_Text('email_users');
$email_users->setLabel('Email:')
->setRequired(false)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidators(array(
array('EmailAddress', true,
array(
'messages' =>
array(Zend_Validate_EmailAddress::INVALID => 'Invalid Email')
)
),
array('Db_NoRecordExists', true,
array(
'messages' =>
array(Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND => 'Email already exists'),
'table' => 'users',
'field' => 'email_users')
)));
The functionality is fine, the problem is that when the email is invalid it now shows me the default zend validate messages, ut when it exists it does show me my custom message. Is there any way to archive the previous functionality this way? (Replacing all invalid email messages) I can't uset setErrorMessages since this shows me 'invalid email' when the email alrady exists.
I tried using 'messages' => 'Error' but nothing happens (no errors but the default messages show), I tried:
$emailValidator = new Zend_Validate_EmailAddress();
$emailValidator->setMessages('Invalid email');
And on my form element I added
$email_users->addValidator($emailValidator)
Nothing same results. The closest I have gotten is doing 'messages' => array(Zend_Validate_EmailAddress::INVALID_FORMAT => 'Invalid email') this shows the msg when I type something like 'email#' or 'email' but if I type 'email#host' it shows me 3 erros regarding the hostname, dns and localnetwork, which they don't show when I use setMessages('Error') (just displays 'Error'),
Thanks in advance.
I posted an answer which explains how all the different error message setting functions work here,
Zend validators and error messages: addValidator and addErrorMessage
In short, try this:
'messages' => 'Email already exists'
instead of using an array.
You have to write validator like this..
$email_users->addValidator(
'EmailAddress',
true,
array( 'messages' => array( 'emailAddressInvalidFormat' => "Email Address is Not Valid... !<br>", "emailAddressInvalidHostname"=>"Email Address is Not Valid... !<br>", "hostnameUnknownTld"=>"Email Address is Not Valid... !<br>","hostnameLocalNameNotAllowed"=>"Email Address is Not Valid... !<br>") )
);
In all cases of invalid email address error must show "Email Address is Not Valid... !".