In a controller, I need to check a string to see if it is a valid email address. Is there an existing method in Cake that I can use to check this?
It has nothing to do with models, so I don't want to use a validate array.
The validation package in CakePHP provides features to build validators that can validate arbitrary arrays of data with ease. You can find a list of available Validation rules in the API. Validator objects define the rules that apply to a set of fields.
To check if a string is a valid email address in JavaScript, we can use a regex expression to match the symbols like @,. and the strings in between them.
CakePHP provides empty value support for different shapes of data: allowEmptyString () Should be used when you want to only accept an empty string. allowEmptyArray () Should be used when you want to accept an array. allowEmptyDate () Should be used when you want to accept an empty string, or an array that is marshalled into a date field.
Possible existing error messages defined via the message option will be overwritten by the ones returned from the validation rule method: When defining validation rules, you can use the on key to define when a validation rule should be applied. If left undefined, the rule will always be applied.
I found the core Validation class. Validation::email()
App::uses('Validation', 'Utility');
class MyController extends AppController
{
public function myAction()
{
$isValid = Validation::email('[email protected]'); // Returns true or false
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With