I'm writing a Backend System and I want to allow the users to change their email address.
I've written a custom validator to check if the email-address the user has entered already exists in my database.
Now I ran into a problem: The form is populated with the user data, so his email address is the default value of the email field. Now if the user submits the form, my validators throws an error, because (of course) this email address does already exist!
How can I solve this problem? Maybe a Validator is not the right approach to do this?
Or is there a solution to detect if the user changed the default value and fire the validator only in that case?
Hehe, that's a common problem running into validators the first time. The key is to remove that one id from the validator, inside your validator exclude the current user ID from the clause:
$validator = new Zend_Validate_Db_NoRecordExists(
array(
'table' => 'users',
'field' => 'email',
'exclude' => array(
'field' => 'id',
'value' => $id_to_edit
)
)
);
Edit: for further explanation as to what this does. It still grabs all the email adresses from the database and it still checks if there's a misconflict. If an email exists, it just ignores the email from id=$id_to_edit - so when the user changes its email but another user has that email already, the error gets thrown anyways!
Related
What I have
foreach ($statuses as $key=>$value) {
echo $this->Form->control('Filter.statuses['.$key.']', array(
'type' => 'checkbox',
'value' => $key,
'label' => $value,
));
}
What I'm getting
Unexpected field 'Filter.statuses[1' in POST data
Unexpected field 'Filter.statuses[2' in POST data
Unexpected field 'Filter.statuses[3' in POST data
...
What I have tried
$this->Form->unlockField('Filter.statuses');
$this->Form->unlockField('Filter.statuses[]');
If I remove the Filter. prefix, the errors are gone and I no longer need the unlockField() call.
References
In cakephp 3 I got error Unexpected field in POST data
Unexpected field 'g-recaptcha-response' in POST data on CakePHP 3
https://book.cakephp.org/3.0/en/controllers/components/security.html
You're not supposed to use brackets in the field name, the form helper doesn't support that. If you ever need an unconventional name that the form helper doesn't support, then use the name option to specify it, while passing a compatible field name to the control() method's first argument.
Use the dot syntax all the way:
echo $this->Form->control("Filter.statuses.$key", /* ... */);
That way the form helper will be able to secure the fields, and create proper HTML name attribute values like Filter[statuses][1].
I am creating my first drupal form and i am wondering if its needed to validate the select options? here is the form element
$form['page1']['color']=array(
'#type'=>'select',
'#title'=>t('Select Transmission'),
'#empty_value' => '',
'#options' => $color_options,
'#required'=>TRUE,
'#default_value' => !empty($form_state['values']['color']) ? $form_state['values']['color'] : '',
);
so since drupal have the hidden fields for security can i trust that this form is always sent unaltered from my website?
thanks
Michael
You don't need to validate the select options. Drupal will take care of it for you. If a user tries to alter the value of a option (that is not one of a key or your $color_options array) with Firebug (or whatever), he will get the message "An illegal choice has been detected. Please contact the site administrator." from Drupal.
Furthermore, you don't need to set a value from "$form_state" for the "#default_value" key. Just put one of the key of the "$color_options" for instance or don't use the key at all if you don't need a default value.
I have a question concerning the Laravel 4.1 validators.
$validator = Validator::make(
array('name' => 'Dayle'),
array('email' => 'required|min:5|unique:users')
);
Is it possible to call a specific validation error for the case when the entered email is not unique? Reading the docs I only saw that one is able to define the error message if the validation for 'email' fails. However, if someone enters an email address but this one is already in the database it would be awesome to show the user exactly that he passed "required" but did not pass "unique".
The third parameter of Validator::make() lets you pass in an array of messages. More on this here: http://laravel.com/docs/validation#custom-error-messages
As it says in the above link, you can specify field-and-rule-specific messages by using the dot syntax email.unique. In your case this would be:
$validator = Validator::make(
array('name' => 'Dayle'),
array('email' => 'required|min:5|unique:users'),
array('email.unique' => 'This email is already being used by another user.'),
);
Lets us consider a simple registrations form with fields like username, email, date of birth, password etc.
And there are some data validation rules applied like:
'email'=>array(
'emailrule'=>array(
'rule' => 'email',
'message' => 'You have entered an invalid e-mail address.'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'The e-mail address already exists.',
),
),
When the user submits the form the data gets validated. If invalid the user is redirect to the form and error is shown to the user. But this time since the page has refreshed the values entered by the user have gone.
is there some way to retain those values??
I am using drupal 6. I have a node called [classroom]. I would like to have a [vacancy register] associated with each classroom.
vacancy register is a cck type with:
- uid
- nid
- join date
I would like for each user to [register] for a vacancy. I think I can use flag for this.
When a user joins, I can use rules to action an email to be sent to the user and the [classroom]->cck_email field.
I would like a rule schedule to also run every 30 days ( configurable ) to alert the user to confirm their [registration].
1a. If no registration is confirmed, then 14 days later, the user is [unregistered] from the classroom.
1b. If user confirms registration ( by clicking on a button or url ). then the rule 1 runs again.
I would like to confirms if my approach to this is correct.
Update:
I have been playing around with signup, but there are the rules schedule aspect of it that I find hard customising to my liking.
I'm trying to write a rules event for signup_signup and signup_cancel, then action it through rules schedule. But there a bit of existing signup code I have to look through.
Have to do too much custom work to signup, so I thought, its easier to just do it with rules and flags. The downside is having to also create the UI for it.
For the signup module,
I have the following rules event.
Could this be reviewed please?
<http://drupal.org/node/298549>
function signup_rules_event_info() {
return array(
'signup_signup' => array(
'label' => t('User signups to classroom'),
'module' => 'Signup',
'arguments' => array(
'userA' => array('type' => 'user', 'label' => t('UserA, which adds userB.')),
'userB' => array('type' => 'user', 'label' => t('UserB, which is added to UserA\'s list.')),
),
),
);
}
I don't know what to do with the arguments list.
I haven't looked at the signup module for some time, but I think that might be a better module for your case. The flag module is a good choice too, but the signup module is more geared towards what you are doing. Users signing up for content like a classroom.