Laravel custom validation rule on valid email - php

So basically I have a simple form and one of the fields is an email.
My controller responsible for this form is the following(showing only the essentials)
$messages = array(
'rsvp_email.required' => 'A valid email is required.')
);
$rules = array(
'rsvp_email' => 'required|max:150|email',
);
$validator = Validator::make($request->all(), $rules,$messages);
Now there are 2 scenarios:
a) The email is not inserted and the above validation works with the custom message(This works OK)
b) The email is not in a valid format (myemail#email) and the resulted error message is The rsvp email must be a valid email address. which is not what I want to be displayed.
What additional rule should I include for a valid email?
Thank

IF you want to change this message go to the following path:
resources/lang/en/validation.php
and change value of email index.

Related

email shows # as %40

I am working on Yii and I need to send a confirmation email one sign up. The content shows the email as abc%40#example.com
the email is directly called from the database and added to the mail function
I am not sure how this can be fixed
$activation_url = $this->createAbsoluteUrl('/user/activation/activation',array("activkey" => $registerform->activkey, "email" => $registerform->email));
the above code is the sample
Any help is appreciated
Apologies
the result is shown like this abc%40example.com and not abc%40#example.com
Try this:
$activation_url = $this->createAbsoluteUrl('/user/activation/activation',array("activkey" => $registerform->activkey, "email" => urldecode($registerform->email)));

Laravel form validation - validating against 2 data columns

I have a database table that tracks and email address and a client id. The rule is that and email can only belong to one client id. An example would be that I could add, email#domian.com and the client_id 20 but I would not be able to save that again, and this is where the form validation comes in,
I have the following validation rules in my controller,
$data = Input::all();
$client_id = Input::get('client_id');
$validation = Validator::make(
array('email' => Input::get('email')),
array('email' => 'required|email|unique:emails, email, NULL, client_id, $client_id')
);
if($validation->fails()) {
return Response::json($validation->messages(), 400);
} else {
}
Basically I am saying that the email should be a valid email and it should be unique against all the other emails adress that have the same client id.
However I get a PHP error back,
Undefined offset: 1
The POST that I am sending looks like this,
client_id: "16"
email: "simon#simonainley.info"
involved: 1
project_id: "64"
visible: true

PHP mailing list without form

Is there anyway to get a user's email without a form in php? I want to able to send someone an email for a mailing list and instead of them filling out a form where they enter their email, the php page I have linked in the email. They click the link and it sends them to a thank-you page on the site. Then it stores their IP, date/time and email in a database I have set up. I've completed this with a form, but I'm wondering if there's any possible way to do it without a form. Here's my code for the form, I'm using Drupal form API. I've been googling this for hours, but have found nothing. I saw something about URL parameters, but it didn't really pertain to my question. Any help is appreciated.
<?php
//This custom module will be used on the website to gain consent from our clients because of the recent CASL anti spam laws that were passed in Cnada.
//Menu hook starts here, implements menu and sets the title, url of the page.
function form_casl_menu() {
$items = array();
$items['casl-consent/form'] = array( //this creates a URL that will call this form at "url"
'title' => 'CASL Subscription', //page title
'description' => 'A form that allows us to send emails to clients with their consent.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_casl_form'), //put the name of the form here
'access callback' => TRUE
);
return $items;
}
//permission hook
function form_casl_permission() {
return array(
'administer my module' => array(
'title' => t('Administer my module'),
'description' => t('Perform administration tasks for my module.'),
),
);
}
//form hook, form elements start here
function form_casl_form($form, &$form_state) {
//sometext here
$form['some_text'] = array(
'#markup' => '<p><b>Simply enter your email address to subscribe</b>
</p>'
);
$form['email'] = array(
'#type' => 'textfield', //their email
'#title' => 'Email:',
'#size' => 30,
'#maxlength' => 150,
'#required' => TRUE,
);
//submit button
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Submit Data'),
);
return $form;
}
//validate hook
function form_casl_form_validate($form, &$form_state) { //invalid email error
if (!valid_email_address($form_state['values']['email'])) {
form_set_error('mail', t('You must enter a valid e-mail address.'));
}
}
//submit hook
function form_casl_form_submit($form, $form_state) {
$sDate = date("Y-m-d H:i:s"); //returns the date and time
global $name;
$subbed = 'Yes';
//----------------------------------------------------------------\\
$ip55 = &drupal_static(__FUNCTION__);
//ip get function, returns a clients IP address and checks if they're behind a proxy.
if (!isset($ip55)) {
$ip55 = $_SERVER['REMOTE_ADDR'];
if (variable_get('reverse_proxy', 0)) {
$reverse_proxy_header = variable_get('reverse_proxy_header', 'HTTP_X_FORWARDED_FOR');
if (!empty($_SERVER[$reverse_proxy_header])) {
// If an array of known reverse proxy IPs is provided, then trust
// the XFF header if request really comes from one of them.
$reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
// Turn XFF header into an array.
$forwarded = explode(',', $_SERVER[$reverse_proxy_header]);
// Trim the forwarded IPs; they may have been delimited by commas and spaces.
$forwarded = array_map('trim', $forwarded);
// Tack direct client IP onto end of forwarded array.
$forwarded[] = $ip55;
// Eliminate all trusted IPs.
$untrusted = array_diff($forwarded, $reverse_proxy_addresses);
// The right-most IP is the most specific we can trust.
$ip55 = array_pop($untrusted);
}
}
}
//-----------------------------------------------------------------\\
//inserting data into database
db_insert('CASL')
->fields(array(
'email' => $form_state['values']['email'],//email
'ip' => $ip55,//ip
'substatus' => $subbed,
'datetime' => $sDate,//date and time
))->execute();
//sending confirmation email to the user, letting them know they can unsub at any time.
$values = $form_state['values'];
$to = $form_state['values']['email'];
$subject = 'Confirmation';
$message ="Thank you for your submission. You may unsubscribe at any time by refilling out this form http://www.localhost.ca/casl-consent/form and selecting the 'unsubscribe' option. Or, you can simply email us a request to unsubscribe, and we will remove you from our database immediately.
If you have any questions or concerns, you can email us at this link: http://www.localhost.ca/contact";
mail($to, $subject, $message);
drupal_set_message("Thank you! Your information has been received successfully and you have been sent a confirmation email.");
//thank you message after submission
}
?>
Based on your comment, it sounds like you are attempting to associate a known email address to an IP address. You say you are sending an email and they are clicking a link. I'm making the assumption that each of these links on each email is uniquely generated an associated to a specific address. In that case, your problem comes down to "How do I find a client's IP address?".
In PHP, you can do this using $_SERVER['REMOTE_ADDR']. This is the most reliable, but by no means fool proof.
'REMOTE_ADDR'
The IP address from which the user is viewing the current page.
If the user is behind a proxy, the $_SERVER['HTTP_X_FORWARDED_FOR'] may have been set, but this value can be spoofed by both the client and the proxy. It's not guaranteed to be accurate and shouldn't be trusted. If the user if using a proxy, though, the value in REMOTE_ADDR will be the IP address of the proxy that hits your webserver, not that of the client.
So, assuming you are going to use REMOTE_ADDR, how do you associate the email address to the IP address?
The user clicks a unique link that you have stored with the known email address. That link runs a very simply script and looks at REMOTE_ADDR and maybe HTTP_X_FORWARDED_FOR and you store those two values.
Your table should be as simple as this:
email_address | unique_link | remote_addr | http_x_forwarded_for
The last two values are filled out once the user clicks the unique_link. How you populate email_address initially, remains your secret.

Laravel 4 validation message attributes

I'm running into an issue with custom validation messages. Here's my code:
$input = array(
'email' => Input::get('email'));
$messages = array(
'email.required' => 'The darn email is required man!',
'email.exists' => 'That email address is already being used!',
'email.email' => 'Bad email, Bad.');
$rules = array(
'email' => 'required|exists:users,email|email');
My passback to the Laravel view is:
#foreach( $errors->all() as $message )
<li>{{ $message }}</li>
#endforeach
With this, I enter no email address and get:
"The darn email is required"
... Good!
Then I enter an email address that exists in the DB:
"That email address is already being used!"
... Good!
Then I enter an invalid email address like "noemail":
"That email address is already being used!"
"Bad email, Bad."
Why am I getting two messages when I should only be getting the one associated with the email.email custom attribute? (i.e., the "Bad email, Bad." message)?
Thanks!
Figured it out....
needed to be using the unique:table,column option, not exists.

Zend form change matching error

I add validator to email element:
$usr_email->addValidator('Db_NoRecordExists', true, array('users', 'usr_email'));
And now have error message: A record matching 'admin#example' was found
I want change this error, but I want that will display inserted email
A email 'admin#example' already exist in database
I try , but email not disply ant html tags
addErrorMessage("**This email **strong text** address already exist.**");
Use %value% placeholder in the error message, it will be replaced with the actual value.
Example:
$usr_email->addValidator('Db_NoRecordExists', true, array(
'users',
'usr_email',
'messages' => array(
'recordFound' => 'A email %value% already exist in database'
)));

Categories