ZF2 form validation not working properly for email - regex

i have a input box with type="email" and validation it with zend validator
<input type="email" name="email" > email </input>
'email' => array(
'required' => true,
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern'=>'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
'messages' => array(
\Zend\Validator\Regex::NOT_MATCH=>'Please fill correct email ',
)
),
'break_chain_on_failure' => true
),
),
problem is if i use another array to check for
IS_EMPTY
zend regex again does not display the above error instead display default zend regex error and if i remove IS_EMPTY then it works fine. regex error is
1. input does not match to expression '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/'
EXACTLY NOT ABLE TO UNDERSTAND WHY THIS WORKS FINE WITHOUT EMPTY CHECK AND DOESN'T WORK ALONG WITH IT

You don't need to use notEmpty, you only need to set the field as "required" and specify the error message:
$this->add(array(
'name' => 'email',
'required' => true,
'error_message' => 'Please entry e-mail.',
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array (
'messages' => array(EmailAddress::INVALID => 'Please specify a valid e-mail.'),
),
'break_chain_on_failure' => true,
),
),
));

I have also the same dilemma where this "input does not match to expression" always appears instead of the message for EmailAddress::INVALID_FORMAT. But I found that the code you posted fixed the same error I had. This is my code.
'validators' => array(
array (
'name' => 'Regex',
'options' => array(
'pattern'=>'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
'messages' => array(
Regex::NOT_MATCH => 'Please provide a valid email address.',
),
),
'break_chain_on_failure' => true
),
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
EmailAddress::INVALID_FORMAT => 'Please provide a valid email address.',
EmailAddress::DOT_ATOM => '',
EmailAddress::INVALID_FORMAT => '',
EmailAddress::INVALID_LOCAL_PART => '',
EmailAddress::QUOTED_STRING => '',
)
),
),
),

Related

Compare user ID exactly, not with LIKE

I have this query -
<?php
$post_args = array(
'post_type' => 'tribe_events',
'eventDisplay'=>'custom',
'start_date' => date( 'Y-m-d H:i:s', strtotime( '-365 days' ) ),
'tax_query' => array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'slug',
'terms' => 'compliance',
),
),
'meta_query' => array(
array(
'key' => 'associated_people',
'value' => $current_user,
'compare' => 'LIKE'
)
)
); ?>
At the moment it is fetching events that are associated to a particular person in a custom post type called people. I'm doing this using a relationship advanced custom field called associated_people.
I'm using the 'compare' => 'LIKE' code to do this also, but it's fetching other id's that are like the current one. e.g. If it's looking for ID 117, it's also bringing back 17, 11, 7..etc.
Is there a way to look at just the exact ID?
So far we have tried changing it to 'compare' => '='
Any help would be much appreciated. Thanks!
We solved it by changing the way the value was called -
<?php
$post_args = array(
'post_type' => 'tribe_events',
'eventDisplay'=>'custom',
'start_date' => date( 'Y-m-d H:i:s', strtotime( '-365 days' ) ),
'tax_query' => array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'slug',
'terms' => 'compliance',
),
),
'meta_query' => array(
array(
'key' => 'associated_people',
'value' => '"' . $current_user . '"',
'compare' => 'LIKE'
)
)
); ?>

Regex Input Filter in zend framework 2

i add input filter by this parameters :
and the question is why zend framework 2 have problem with utf8 pattern
$formInputFilter->add($inputFactory->createInput(array(
'name' => $field->attribute->id,
'required' => true,
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^[0-9,\x{0600}-\x{06FF}]*/u',
'messages' => array(
'regexNotMatch'=>'number is not correct'
),
),
),
),
)
)
);
the error is :
preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 15
I use this in my urls. It works with any digits and characters
'\/^\d{4},(?:\s|\w)+\/u*'
https://regex101.com/r/mW4xJ4/2
\/^\d(?:\s|\w)+\/u*
You can see the explanations of each character here: https://regex101.com/r/mW4xJ4/1

Zend Framework 2 child regex and literal routing

Based on this stackoverflow question 15077075 i have accomplished that my application has a regex based route so i can pass this to my view and edit action.
app/123 - app controller view action (get by article id)
app/name-of-article - app controller view action (get by article name)
app/123/edit - app controller edit action (article id)
app/name-of-article/edit - app controller edit action (article name)
app/search/{search-string} - app controler search action (currently only accepts a searchstring without spaces and special characters)
This i have accomplished with the code below and is similar to the code in the link above:
'app' => array(
'type' => 'literal',
'options' => array(
'route' => '/app',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'index',
),
),
'may_terminate' => true,
),
'view' => array(
'type' => 'regex',
'options' => array(
'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'view',
),
'spec' => '/app/%view%',
),
'priority' => -1000,
),
'edit' => array(
'type' => 'regex',
'options' => array(
'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)/(?<edit>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'edit',
),
'spec' => '/app/%view%/%edit%',
),
'priority' => -1000,
),
I have twoone issue
the first is that the url viewhelper doesn't recognize my routes
$controller = app
$action = recent
$this->url( $controller, array( 'action' => $action ) )
it just prints /app instead of /app/recent,
The same occurs when $action = 'search' or $action = 'new' it only prints /app
the second is that the search is recognized to its controller action only it fails when i put spaces or special characters in it
when i try to add \s in the constraints of the searchkey ( '[\sa-zA-Z0-9_-]+' ) it routes to the edit function
the route of search looks like this
Edited the route to this and it worked
'search' => array(
'type' => 'literal',
'options' => array(
'route' => '/app/search',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'search',
),
),
'may_terminate' => true,
'child_routes' => array(
'search' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:searchkey]',
'constraints' => array(
'searchkey' => '[a-zA-Z0-9_\+-]+'
),
'defaults' => array(
'action' => 'search'
),
),
),
),
),
i hope it's clear what i want if any code snippits are needed please ask
Sorted it out!
If one needs to get routes in his application this way you can use the code below
/App App/Index
/App/name-of-article App/View
/App/123 App/View
/App/name-of-article/edit App/Edit
/App/123/edit App/Edit
/App/recent App/Recent
/App/search App/Search
/App/new App/New
In my module.config.php i changed the route to:
'app' => array(
'type' => 'segment',
'options' => array(
'route' => '/app[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'index',
),
),
'may_terminate' => true,
),
'search' => array(
'type' => 'literal',
'options' => array(
'route' => '/app/search',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'search',
),
),
'priority' => 1,
'may_terminate' => true,
'child_routes' => array(
'search' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:searchkey]',
'constraints' => array(
'searchkey' => '[\sa-zA-Z0-9_\+-]+'
),
'defaults' => array(
'action' => 'search'
),
),
'priority' => 2,
'may_terminate' => true,
),
),
),
'new' => array(
'type' => 'literal',
'options' => array(
'route' => '/app/new',
'defaults' => array(
'controller' => 'App',
'action' => 'recent',
),
),
'priority' => 2,
),
'view' => array(
'type' => 'regex',
'options' => array(
'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'view',
),
'spec' => '/app/%view%',
),
),
'edit' => array(
'type' => 'regex',
'options' => array(
'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)/(?<edit>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'edit',
),
'spec' => '/app/%view%/%edit%',
),
),
If you need other routes you can just add a new literal route below te app route, make sure you don't forget to set the priority

Cakephp validating if my user checked at least one option

I have a problem validating if my user checked at least one option from a list of checkboxes.
Here is what i tried:
My view looks like this:
echo $this->Form->input('market_segment_targeted', array(
'multiple' => 'checkbox',
'label'=>array('text' => 'Market segment targeted', 'class'=>'w120'),
'options' => array(
'Home users' => 'Home users',
'SOHO' => 'SOHO',
'SMB' => 'SMB',
'Enterprise' => 'Enterprise'
),
));
In my controller i have added this snippet of code:
$validate_on_fly = array(
'market_segment_targeted' => array(
'notEmpty' => array(
'rule' => array('multiple', array('min' => 1)),
'required' => true,
'message' => 'Please select at least one!'
))
)));
$this->Partner->validate = Set::merge(
$this->Partner->validate,
$validate_on_fly
);
Any ideas what am i doing wrong?
Thank you
In CakePHP you can use Model Validation for checkboxes. Here is a quick example.
Your Form can look like:
$this->Form->create('User');
$this->Form->input('User.agree', array('type'=>'checkbox', 'hiddenField'=>false, 'value'=>'0'));
$this->Form->submit('Save'):
$this->Form->end();
Then in your Model under public $validate, use:
'agree'=>array(
'Not empty'=>array(
'rule'=>array('comparison', '!=', 0),
'required'=>true,
'message'=>'You must agree to the ToS'
)
)

ZF2 Doctrine2 Form validation NoObjectExists

I'm using \DoctrineModule\Validator\NoObjectExists to validate my form submission. But, like the ZendDB adapter, I need to exclude all the rows where FieldX equals to SomethingX.
How can I achive that?
This is my form, filter input:
$filter->add(
$factory->createInput(array(
'name' => 'example',
'required' => false,
'validators' => array(
array(
'name' => '\DoctrineModule\Validator\NoObjectExists',
'options' => array(
'object_repository' => $this->mainRepository,
'fields' => 'example'
),
),
),
))
);
Thanks.