sonata_type_collection changing tab on validation - sonata-admin

i have a problem on validation:
i have an admin ( scuolaAdmin) with sonata_type_model in a tab:
->tab('docenti')
->add('docentiScuole','sonata_type_collection',array('error_bubbling' => true,'by_reference' => false , 'required' => false, 'label' => $dictionary->getLemma("aggiungiDocente") ), array( 'edit' => 'inline','inline' => 'table'))
->end()
the admin docentiScuoleAdmin has a validation
public function validate(ErrorElement $errorElement, $object)
{
$errorElement
->with('docenti')
->assertLength(array('max' =>10))
->end()
;
}
everythings works but if the validation fails, the correct text is displayed under docenti field, but the page doesn't show the correct tab ('docenti')
but it show the first tab
there is a way to display the correct tab?
thank you

Related

Drupal 7 - how do I create a custom registration form

I am new to Drupal development. I want to create a registration form for visitors of my site. Default registration page only has two fields: Username and Email address.
How can I add more fields to it, e.g password, picture and timezone. I also want to capture tow other information - Date of Birth and Gender. These two fields are not available in default users table. How can I tie these information with a user account? Do I have to create a new table and put these information there referencing the uid of user table? If it is possible how can I pull the joined record?
Is there any possibility that I create a new content type for this purpose but records still go to default users table and can be used for login?
If 2 above is not possible I probably have to use hook_form_alter but where should I put this function?
When creating a custom registration form shall I use default registration page i.e. /user/register and customize it?
I am sorry if above questions look very childish and silly! Hope you will consider my my newbie status. If possible please help me with a step by step solution.
Thanks!
UPDATE
To accomplish the requirement I created a custom module called user_signup and in user_signup.module file I have written the following code.
<?php
/*
Implements hook_menu()
*/
function user_signup_menu(){
$items = array();
$items['user/signup'] = array(
'title' => 'Sign Up',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_signup_registration_page'),
'access arguments' => array('access content'),
);
return $items;
}
function user_signup_registration_page($form, &$form_state){
$form['name'] = array(
'#title' => 'Username',
'#description' => 'choose a username',
'#type' => 'textfield',
'#required' => TRUE,
);
$form['mail'] = array(
'#title' => 'Email',
'#description' => 'enter a valid email address',
'#type' => 'textfield',
'#required' => TRUE,
);
$form['pass'] = array(
'#title' => 'Password',
'#description' => 'Enter a strong password',
'#type' => 'password',
'#required' => TRUE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create Account'),
);
return $form;
}
function user_signup_registration_page_submit($form, &$form_state){
$new_user_id = db_insert('users')
->$fields(array( **//this is line number 45**
'name' => $form_state['values']['name'],
'mail' => $form_state['values']['mail'],
'pass' => $form_state['values']['pass'],
))
->execute();
drupal_set_message(t('New user created'));
}
Everything works perfectly but when I hit the submit button I am getting this error:
Fatal error: Method name must be a string in D:\xampp\htdocs\imdbind\sites\all\modules\user-signup\user_signup.module on line 45
I have marked line number 45 in above code snippet as **//this is line number 45**. I did not find any difference when comparing my code with theirs. What I am doing wrong?
Just change line 45 with following
From:
->$fields(array( **//this is line number 45**
To
->fields(array( **//this is line number 45**
You can add Fields here /admin/config/people/accounts/fields int eh People Account settings Manage Fields form.

How to perform any action before submit form in cake php

I have a form in cake php Here i have some hidden field that i want to sent when user click on submit button.
At start hidden field value is blank you can see in given form below
I set that hidden field value in playTimer() function in js file
But my prob is when i am going to submit that form it have blank value for actionId hidden field in post data .Wen i again clcik submit then i have value .
I want to set it when user first clcik the submit button so taht i am using before call back function.
echo $this->Form->create('Meditation', array('id' => 'timerform'));
echo $this->Form->hidden('actionId', array('value'=>'', 'id'=>'actionId'));
echo $this->Js->submit('Play', array(
'before' => 'playTimer();',
'update' => '#map_container',
'complete' => 'setsessionid();',
'success' => 'soundPlay();resume();',
'class' => 'btn btn-med-success playTimer',
'div' => false,
'async' => false,
'url' => array('action' => 'timerupdateDuo')
));
==========Custom.js============
function playTimer(){
$("#actionId").val('playTimer');
}
Thanks
<? echo $this->Form->submit(__('Save'), array(
'class' => 'ClassOfTheFormSubmitBTN'
)); ?>
<script type="text/javascript">
$(function(){
$('.ClassOfTheFormSubmitBTN').on('click',function(){
// implement your logic here
$('#MODELNAMEactionId').val('something');
//you can use ajax here with serialize to send the form
//to stop the form from posting return false;
});
});
</script>

hook_permission and control access [how to redirect to the login page if user not logged in]

How does one limit "what" can access a page. I have little understanding on how to use hook_permission() to set user permission based on a role to control access to hook_menu() items. However, what if I have a hook_menu() item that is to be used solely by the system itself? For example, say I have a registration page in a custom module: module/register - Anyone can access that page. Then, say I have an another page which is only for admin. So, I register another item in hook_menu(), call it module/register/reg_user_details - I do not want anyone to be able to browse to module/register/reg_user_details
Instead I want page to be visible only when user is logged in, I have already created a hook_permission
/**
* Implements hook_menu()
*
*/
function video_subtitles_menu() {
$items = array();
$items['video_subtitles/upload'] = array( //this creates a URL that will call this form at "video_subtitles_test/upload"
'title' => 'Upload Subtitle', //page title
'description' => 'Uploading subtitle for videos',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, drupal_get_form need to be used
'page arguments' => array('video_upload_subtitles_form'), //Name of the Uplaod Form
'access callback' => 'user_access',
'access arguments' => array('administer video_subtitles module'),
);
$items['player/video_subtitle_status'] = array(
'page callback' => 'video_subtitle_status',
'access callback' => 'user_access',
'access arguments' => array('administer video_subtitles status'),
);
return $items;
}
/**
* Implements hook_permission.
*/
function video_subtitles_permission() {
return array(
'administer video_subtitles module' => array(
'title' => t('Administer video_subtitles module'),
'description' => t('Access the video_subtitles upload module Page'),
),
'administer video_subtitles status' => array(
'title' => t('Administer video_subtitles module status'),
'description' => t('Access the video_subtitles module status Page'),
));
}
So User is not able to access my page unless user is logged in, but
what I need user should be redirected to the login page when he is not logged in as admin , if he successfully login it should be redirected to my page.
visit url d-7/example/my-module
if already logged in show the page
if not logged in redirect to the login page
if user logged in successfully redirect to d-7/example/my-module page
What is best way to achieve this
Similar Question
using-hook-menu-with-hook-permission-access-denied
using-hook-menu-and-hook-permission-to-control-access
can-someone-explain-access-arguments-in-drupal

Cakephp 2.3.9 custom message in model validation not working

I am doing model validation in my admin panel login so there is only two fields username and password. Validation is working but custom message which I have written in my model is not shown.
Model
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please Enter Your Username'
)
),
'password' => array(
'required' => array(
'rule' => array ('notEmpty'),
'message' => 'Please Enter Your Password'
)
)
);
Controller
function login(){
$this->layout = 'admin_login';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
View
echo $this->Form->create('Admin',array('autocomplete'=>"off"));
echo '<div style="width:294px;float:left;position:relative;">';
echo $this->Form->input('username' , array('label' => '', 'placeholder' =>'Enter your username','div' => false));
echo $this->Form->input('password' , array('label' => '', 'value' =>'', 'div' => false,'placeholder'=>'Enter Your Password'));
echo '</div>';
echo '<div style="padding-left:0px;">';
echo $this->Form->end(__('Login' ,true));
I have already tried a few things like which is mentioned in this link, but it's not working for me.
CakePHP : Validation message not displaying
That looks like a message from the browser and not CakePHP.
CakePHP now adds a required attribute which modern browsers can use to trigger an error.
You can do one of three things here:
One: Set up your form to leave validation to the server:
$this->Form->create(array('novalidate'=>true));
Two: Set a custom validation message in the browser: http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#dom-cva-setcustomvalidity
Three: tolerate it
You get that message because the "username" field is flagged as "required". Maybe you've not defined it in the Form->input() function, but the "required" flag has been automatically added from the Model (due to your validation rules). As timstermatic said, it's a browser validation message caused by the required attribute.
To solve this issue (and show the CakePHP validation message) you've to force for avoiding the addition of the "required" flag on your field:
$this->Form->input('username', array('required' => FALSE));
This will override the Model automatic additions. Happy coding ;)
*Edited => It's important to clarify that the inline override removes only the required flag on the field: you'll take advantage of the Model validation anyway (just because if an empty field is sent to the server, it will not pass the validation rule you entered.
keep this code it will bypass the html5 validation and add your custom validations
view
echo $this->form->create('Post',array('action'=>'add'));
echo $this->form->input('title');
echo $this->form->input('body');
echo $this->form->submit('Save Post',array('formnovalidate'=>true));
echo $this->form->end();//Creates ending form tag
Model
var $validate=array(
'title'=>array(
'title_must_not_be_empty'=>array('rule'=>'notEmpty','message'=>'Please enter a title),
'title_must_be_unique'=>array('rule'=>'isUnique','message'=>'Title name already exists')
),
'body'=>array(
'body_must_not_be_empty'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter body'
)
)
);
This will work just the way you want

How do I make certain menu items visible to certain roles?

In /drupal/admin/structure/menu/manage/main-menu, I have some links that I only want displayed for user's that have a specific permission.
How would I go about this?
In my module, I have
...
$items['resume/joblist'] = array(
'page callback' => 'ac_resume_job_list',
'access arguments' => array('view joblist'),
'title' => 'Job List',
'description' => 'Job List',
);
...
function ac_resume_permission()
{
return array("view joblist" => array("title" => "View Job List"));
}
When I go to "resume/joblist" under a user without the permission, I get the "Access Denied" as expected, however the link is still displayed.
hook_perm() was renamed to hook_permission() in Drupal 7, and there's a bit of a mismatch between view mylink that you define and view joblist that you declare as a permission.
You could change your code to look more like this:
function mymodule_menu() {
$items['mylink'] = array(
'page callback' => 'mymodule_mylink',
'access arguments' => array('view mylink'),
'title' => 'My Link',
'description' => 'My Link',
);
return $items;
}
function mymodule_permission() {
return array(
'view mylink' => array(
'Title' => 'View My Link'
)
);
}
After you clear Drupal's cache navigate to admin/people/permissions and assign your new permission to the role you want to be able to access the page you define in hook_menu().
Once you've done that users with that role will be able to access the page :)
I recommend using the following module: Menu Item Visibility, it does exactly what you need.

Resources