Guys folowing cakephp3 documentation for validation i try to validate my email field but that validation not work good.
How this can be valid email?
a#53%#sdasd#gmail.com
%321&%$sd#gmail.com
Form is successfull submited and data is saved to database. I think this validation is not good bcs i never see in my live email address like this above.
return $validator->requirePresence('email')
->notEmpty('email', __('Email is required field'))
->add('email', 'validFormat', [
'rule' => 'email',
'message' => 'E-mail must be valid'
]);
Here you can find regular expression that Cake uses for email validation:
https://github.com/cakephp/cakephp/blob/master/src/Validation/Validation.php#L567
If it does not satisfy your needs you can always provide your own.
Take a look on Email validation rule in CakePHP sources:
public static function email($check, $deep = false, $regex = null)
You can provide your own regular expression like this:
$validator->add("email", "validFormat", [
"rule" => ["email", false, $yourRegexp],
"message" => "Email must be valid."
])
Related
I'm with Laravel and I want to write elegant validation rules :) With this Framework its really easy, but I don't know how to approach this when facing 1:n relationships.
I have two Resources, User and Contact. An User can have multiple Contacts.
So, I want a Form where you can fill all User's fields AND all Contact's information.
To do that, I would like to write a Request like this:
UserRequest:
public function rules()
return [
'name' => 'required|string',
'email' => 'required|email|unique:exists:users,id',
'contacts' => 'array',
'contacts.*' => new ContactRequest() // This is the problem
]
My question is: How can I apply this type of validation? Specifically when using array, how can I make a Modular Validation to apply validations of nested Resources? Or should I develop a ContactRule instead?
Edit:
I want that front end send form like this:
` // POST: users
{
'name': 'UserName',
'email': 'user#mail.com'
'contacts': [
[
'email' => 'contac_1#mail.com',
'contact_type_id => 1
],
[
'email' => 'contac_2#mail.com',
'contact_type_id => 2
],
}
`
Thats all,
Thx!
We have an API with 100's of results in each request or perhaps post/patch.
We still use:
'data.relationships.users.data.*.id' => [
'string',
'unique:api_groups,name,' . ($this->route('group')->id ?? 0),
]
So for you
'contacts.*.email' => 'required|email|unique:exists:users,id'
Works perfectly. It doesn't get more complex or anything.
I have a registration form which has an input field for email confirmation.
<?= $this->Form->input('cemail',array('label'=>'Confirm E-mail')); ?>
In the UsersTable class inside validationDefault function i am trying to validate this field
$validator
->email('cemail');
The form is not submitted though. Do i have to define this field anywhere else ?
Try
$validator
->requirePresence('cemail')
->add('cemail', 'validFormat', [
'rule' => 'email',
'message' => 'E-mail must be valid'
]);
and in controller
if ($Model->save($data)) {
// data saved
}else{
debug($Model->errors());
}
How can I add custom option to formmMapper in sonata admin class?
I have form related to entity in admin class. For some reason I want to add my own options to one of the field
$formMapper
->with('tab.dimension')
->add('dimension', 'collection', array(
'type' => 'dimension_product',
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'my_custom_options' => false,
))
->end();
Unfortunatly it is not possible in this way,because this options isn't defined in resolver.
But I have no access to resolver in "normal way".
Sonata defined form builder in two methods:
public function getFormBuilder()
{
$this->formOptions['data_class'] = $this->getClass();
$formBuilder = $this->getFormContractor()->getFormBuilder(
$this->getUniqid(),
$this->formOptions
);
$this->defineFormBuilder($formBuilder);
return $formBuilder;
}
public function defineFormBuilder(FormBuilder $formBuilder)
{
$mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this);
$this->configureFormFields($mapper);
foreach ($this->getExtensions() as $extension) {
$extension->configureFormFields($mapper);
}
$this->attachInlineValidator();
}
Allowed options are defined in this object:
new FormMapper($this->getFormContractor(), $formBuilder, $this);
Could somebody give me advice how can i add my own option?
Little bit late to the party, but it sort of depends on what you want to do with this option.
If you need to add a real custom form option, it is not very much different from working directly with Symfony forms. You can add extra options and functionality to a given form type using a form extension . You could even add functionality to the sonata form types this way.
If you simply need to pass an option from one Admin to a child Admin (which I think you might want to do), you can use the field description options rather than the actual form options:
$formMapper
->with('tab.dimension')
->add('dimension', 'collection', array(
'type' => 'dimension_product',
'allow_add' => true,
'allow_delete' => true,
'required' => false,
), array(
'my_custom_options' => false,
))
->end();
Now in your child Admin you can retrieve these options using
$this->getParentFieldDescription()->getOptions();
To be used to configure your child Admin.
I'm working on some admin functions to my site where a user with admin privileges is able to open a form for a given user ID. From this form they can change user permissions, alter the user password or other means.
However, when the form is constructed from the model, the password field pulls the hashed password from the database and populates the password field. As a result, when the admin goes to save the form the hashed password is treated as a plaintext and therefore hashed again, overwriting the original password.
What I need is some way to allow admin users to change the form but only have passwords hashed and updated on the database in the event that it is changed.
My thoughts are to construct the form setting password to blank:
view/User/edit.ctp:
echo $this->Form->input('User.password',array(
'value' => '',
'type' => 'password',
'autocomplete' => 'off'
)
);
And have some sort of check on the save to skip the password; but this is where I'm stuck.
controller/userscontroller.php
public function edit($id = null)
{
$this->User->id = $id;
if ($this->request->is('get'))
{
$this->request->data = $this->User->read();
} else {
//Something here????
if ($this->User->save($this->data))
{
$this->Session->setFlash('Your user has been updated.');
$this->redirect(array('action' => 'index'));
} else
{
$this->Session->setFlash('Unable to update your user.');
}
}
$this->set('groups',$this->User->Group->find('list'));//, array( 'fields' => array('id', 'name'))));
$this->set('sites',$this->User->Site->find('list'));//, array( 'fields' => array('id', 'name'))));
}
How do I check this and prevent the password from updating when there is no change?
Decided Solution:
As per the answers provided I used a second form on the same page that re-uses the signup validation that users go through. When updating site/group privileges the users are sent through one form while passwords through another.
I would build two admin forms, one for changing permissions and the other for updating the password only. While you are at it, the change password form should have a second field for validating the change.
There are some CakePHP plugins to help with managing users and specifically passwords.
I always create a new form specially for changing passwords. You should replace the password field with a link to change the password.
Alternatively, you could disable the input field and require a button to click and use javascript to remove the disabled attribute on the input element
echo $this->Form->input('User.password',array(
'value' => '',
'type' => 'password',
'autocomplete' => 'off',
'disabled' => true
)
);
Jquery because it's easy
$(function(){
$('UsersPassword').click(function(){
$(this).attr('disabled', '0');
});
});
I want to validate fields with Cakephp model validation, without saving the data, for which i am using the following code in controller.
$this->Model->set($this->data);
if ($this->Model->validates()) {
......
}
But here i want to validate only some specific field like 'email_field' and one of its rule 'email'. In model i have specified some other rules for 'email_field' like 'unique' and 'notempty' but i don't want to validate those rules.
How can it be achieved ?
The above will work definitely but it's not an elegant solution when cake has already documented how to validate specific fields of a Model.
if ($this->Model->validates(array('fieldList' => array('field1', 'field2')))) {
// valid
} else {
// invalid
}
For more detail see cookbook
you have different options
you can dynamically unset those other rules:
unset($this->Model->validate['field']['someRuleName']);
or you can assign a completely new rule set to this field
or you can use a different "nonexistent" field for this validation, e.g. "some_other_field" with special rules.
......
$this->Registry->validate = array(
'email_field' => array(
'between' => array(
'rule' => array('between', 10, 100),
'message' => 'Your custom message here',
'required' => true,
),
)
);
$this->Model->set($this->data);
if ($this->Model->validates(array('fieldList' => array('email_field')))) {
......
}