I am trying to pass a string named "publicUsers" in a callback function, but when I display the argument in my console I see the following string... &Z34Sf{;Cr(m
I have included a trait named "VerifyLogin" in my controller which has a function named VerifyAndSetSession
Any help would be really appreciated
below is my code
if(isset($_POST['sign_in']))
{
$this->emailPassValidation("","|callback_VerifyAndSetSession[publicUsers]");
}
This is emailPassValidation method""
function emailPassValidation($new_account_call_back,$sign_in_callback ){
$this->form_validation->set_rules("password","password","required{$sign_in_callback}",
['required' => 'Please enter your %s']);
}
This is where I am debugging the passed arg
trait VeirfyLogIn
{
function VerifyAndSetSession($infoType)
{
fb($infoType, "Infotype");
//This shows in the console.log &Z34Sf{;Cr(m ???
exit;
}
}
Form validation callbacks accept by default one parameter which is the value of the input to check.
It means that :
$this->form_validation->set_rules("field","field","callback_checkField");
Will imply a callback function like this :
public function checkField($field_value){}
If you want to pass extra data to your callback, you can set that value like you did with brackets :
$this->form_validation->set_rules("field","field","callback_checkField['othervalue']");
That new var will not replace the default parameter but add a new one :
public function checkField($field_value, $othervalue){}
So in your case, for :
callback_VerifyAndSetSession[publicUsers]
the function should be :
function VerifyAndSetSession($fieldvalue, $infoType)
Related
I've build a custom field with several values. I've to make this field required. But I want to pass the validation if at least one field is filled and the last one is empty.
But my problem is Drupal warn me that the last (empty) field is required. I've thought that the hook_field_is_empty() solved the problem, but, even if return true, the form cannot be validated.
Many thanks for your help.
Implementation :
function MYMODULE_field_widget_form(...) {
$element['address']+=[
...
'#required' => $instance['required'],
];
...
}
function MYMODULE_field_is_empty($item, $field) {
if (empty($item['address']) && empty($item['other'])) {
return true ;
}
return false ;
}
To solve this problem, I've made my field not required (in the definition of the node's fields). Then, I added a callback in the #validate using the hook_form_alter(). In that callback, I test if at least one field is defined and call a form_set_error() if none is defined.
This makes sense because it's not the field itself that could know all the data of the node. But, something is a bit wrong because it's not possible to mark this field as required (e.g. asterisks).
<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($formid == ...) { // a specific case
array_unshift($form['#validate'], '_MYMODULE_validate_form') ;
}
}
function _MYMODULE_validate_form(&$form, &$form_state) {
if (empty($form_state['values']['field_geo'][LANGUAGE_NONE][0]['address'])) {
form_set_error('field_geo', t('You have to define at least one address.')) ;
}
}
?>
I have a form in Laravel that when it submits, the function donorUpdate is going to be called, and it will save the data to the database. Here is the exact code of the function.
My aim is, the form contains the field name DonorPrefix that when it's empty, the default value should be Mx.. I have tried the code below, and it doesn't work.
public function donorUpdate(Request $request, Donor $donor){
if($request->has('DonorPrefix') == false || $request->DonorPrefix == '' || $request->DonorPrefix == null){
$request->DonorPrefix = 'Mx.';
}
$donor->update($request->all());
return redirect('/donor/'.$donor->DonorID);
}
This is where the weird part start: My debugging attempt is that I commented the return redirect('/donor/'.$donor->DonorID); line and replace with return $request->all(), the value for DonorPrefix is "" (Empty string).
However when I tried return $request->DonorPrefix, the prefix Mx. returned.
Why isn't the value of DonorPrefix is Mx. on $request->all(), but is Mx. on return $request->DonorPrefix?
Edit: I'm certain that other parts of code works like charm. Things are smooth untill I tried adding this 'default value' function.
You can't set request parameters like $request->DonorPrefix = '...'.
To set a parameter on the request you need to use offsetSet like this:
$request->offsetSet('DonorPrefix', 'Mx.');
Also, the has() method already ensures that it is a non-empty string so you can remove some of your checks, and simplify your code like this:
if(!$request->has('DonorPrefix')){
$request->offsetSet('DonorPrefix', 'Mx.');
}
I've got problem with field validation.
I would like to validate form through model. I want to check if field with some value exists.
I would like to block using some titles more than once.
For example
if field "Site" with title "Main" exists in database, you can't validate form.
If it doesn't exist, you can pass it.
I would like to allow user to add just one "Site" with title "Main", but he can add "Site" with any other title in any case.
Have you got some idea how to solve it?
I think you have two options.
(1) Setup an Ajax request to the server.
To do so:
Create a function, that responds to an Ajax request, in your SiteController named checkName()
public function checkName($name) {
// allow ajax requests
$this->request->allowMethod(['ajax']);
// perform your check within the db
$isExistent = [...];
// prepare the response
$response = ['name' => $name, 'isExistent' => $isExistent];
if ($this->request->isAjax()){
$this->autoRender = false;
$this->response->disableCache();
$this->response->type(['json' => 'application/json']);
$this->response->body(json_encode($response));
}
}
Add the route to your routes file with the option '_ext' => 'json'
Prepare your Javascript Ajax function that call the route you have defined and attach it on the onchange attribute of your input field. (see this link for a simple example: http://www.w3schools.com/jquery/ajax_ajax.asp)
(2) Make the 'name' field of the Site table unique.
To do so you could add the following function to your SiteTable class
public function buildRules(
RulesChecker $rules
) {
$rules->add($rules->isUnique(['name']));
return $rules;
}
I have called a custom function on form submit of Drupal User Registration.
Below are the functions that i implemented
/**
* Implements hook_form_alter()
*/
function voen_registration_form_alter(&$form, &$form_state, $form_id) {
//dpm($form);
switch($form_id){
case 'user_register_form':
$form['account']['name']['#required'] = FALSE;
$form['account']['name']['#type'] = 'hidden';
array_unshift($form['#submit'],'voen_generate_username');
print_r($form);
break;
}
}
function voen_generate_username(&$form, &$form_state){
//drupal_set_message('Function Running');
die('Wokring');
}
Now, when the form is submitted i print_r the $form and in submit key i get the following result which is what i expect
[#submit] => Array
(
[0] => voen_generate_username
[1] => user_register_submit
)
But I don't know why my custom function is not executing. I tried with die() as well se drupal_set_message() and also tried to print_r() in custom function but no response. It is still giving me error of 'Please Enter Username' which is comming from user_register_submit function from user module.
Thanks
The submit function is not executed because the validation function is not passed.
Take a look at $form['#validate'], there is an "user_account_form_validate" function which will not pass if you not define at least a default value for name field.
Anyway, if you're trying to allow registration only by email take a look at this module:
https://www.drupal.org/project/email_registration
I have a form :
<?php
$attr = array('id'=>'urlSubmit');
$urlInputAttr = array('name'=>'urlInput','value'=>'yourdomain.com','maxlength'=>'50','size'=>'25');
echo form_open('urlSubmission',$attr);
echo form_input($urlInputAttr);
#echo form_submit('urlInput', '');
echo form_close();
?>
a controller called urlsubmission
$this->load->model('domaincheckmodel');
$this->domaincheckmodel->verifyduplicates($this->input->post('urlInput'));
and a function within a model(domaincheckmodel) which basically checks for duplicate records and inserts a new domain:
function verifyduplicates($tldEntered){
# $_POSTed value of urlInput
## Gather if the domain exists in db
$DupDomains = $this->db->get_where('ClientDomain', array('tld'=>$tldEntered));
if($DupDomains->num_rows() > 0 ){
$this->load->view('err/domainexists'); ##domain already used
}
# else, no domain present, insert.
else{
#array of insert values:
$insertNewDomain = array('tld'=>$this->input->post('urlInput',TRUE));
$this->db->insert('ClientDomain', $insertNewDomain);
$this->load->view('success/domainfree'); ##domain is free and has been entered.
}
}
$this->domaincheckmodel->verifyduplicates($this->input->post('urlInput'));
function verifyduplicates($tldEntered){
# $_POSTed value of urlInput
## Gather if the domain exists in db
$DupDomains = $this->db->get_where('ClientDomain', array('tld'=>$tldEntered));
You're passing from the form to the controller to the model, are you sure the post variable is staying populated? Try the above, capture the post variable in the controller and pass it to the model instead of trying to read it in the model itself?
A little clarification on passing parameters to functions. You can do this via whatever is inside the brackets as follows.
Controller:
$myVariable = $this->someModel->someFunction($someParameter)
Model:
function someFunction($variableIWantToPopulateWithSomeParameter)
So someParameter gets passed from the controller to the function name in the model. There is one thing to be aware of though and that is that model function now EXPECTS a parameter and if you don't give it one, ie you call someFunction() you'll get an error. This can be avoided by giving it a default value like this:
function someFunction($myVariable = 1)
What that is going to do is say if I don't get a value passed to me I am going to make $myVariable equal to one, if I do I'll overwrite 1 with the new value. So if you send two calls to that function this is what you can expect:
//$myVariable is going to be 1, the default.
$this->someModel->someFunction();
//$myVariable is going to be 5, the value passed to it
$this->someModel->someFunction(5);