Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - Utility method for checking a string is a valid email address?

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.

like image 515
BadHorsie Avatar asked Jul 22 '11 11:07

BadHorsie


People also ask

How do I validate an array in CakePHP?

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.

How to check if a string is a valid email address?

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.

What is the use of allowemptystring() in CakePHP?

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.

How do I overwrite existing error messages in validation rules?

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.


1 Answers

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
    }
}
like image 200
BadHorsie Avatar answered Sep 30 '22 00:09

BadHorsie