Get out an error message if noone of checkboxes selected - drupal

I have set a checkboxes form that works perfect, i have this if statement which also works perfect but what i really want is when the user hasn't selectected any checkbox from the list, when he pushes the save button to get out a drupal error that will says "Oh!you do not select nothing"... How i can do this thing?
if (!$selected) {
drupal_set_message(t('You have to select at least one option from the list.'), 'warning');
}

You can set the message manually from your code:
https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_set_message/7
Where you have that your if logic add drupal_set_message() call.

Do you use Form API?
If yes try the #required property
$form['test'] = array(
'#type' => 'checkboxes',
'#options' => $myOptions,
'#required' => TRUE,
'#title' => t('Test'),
)

Related

Symfony - Disable required field if the checkbox is checked

I have a form on Symfony.
My form is composed of a text input and a checkbox.
If the user checks the box, the input text is disabled (I use javascript).
However, if the user doesn't check the box, he has to fill in the input text. If he ticks the box, he does not have to do it.
Here is my form :
$formBuilder
->add('text', TextType::class,array(
'required' => true,
'constraints' => array(
new NotBlank()
)))
->add('box', CheckboxType::class, array(
'mapped' => false,
'label' => 'Box'
))
;
I'm looking for a way to disable text input validation if the box is checked.
How to ignore input text validation if the checkbox is checked?
Do you know how to do this?
Thanks!
You need a custom contraint in this case. You should mark field as non-required and show asterisk (*) for required using javascript. For the backend, you need to create a custom constraint. See symfony documentation here: https://symfony.com/doc/current/validation/custom_constraint.html

Sonata admin, editable field with choice

I'm using sonata admin and there is an option 'editable' => true for edit directly inline datas on the list view.
If my field is a text, it's ok, i can click, edit the text and save directly on the table.
But i don't want an input type="text" when i click on the field, but a list, i'm trying something like :
->add('etat', null, array('editable' => true), 'choice', array(
'choices' => array(
'Brut' => 'Brut',
'NRP' => 'NRP',
)
))
But no effetc.. is this possible ?
Since Sonata Admin Bundle 2.2 choice accepts the "editable" parameter in the list view. You use it like that:
$listMapper->add('etat', 'choice', [
'choices'=>['Brut'=>'Brut', 'NRP' => 'NRP',],
'editable'=>true,
]);
Doc: https://sonata-project.org/bundles/admin/2-2/doc/reference/field_types.html
It's possible for scalar values only. Hear some doc
http://sonata-project.org/bundles/admin/master/doc/reference/field_types.html
Well it's not possible at the moment and won't be possible as I can guess in the near future. Your own realization should be written.

Autocomplete without using a callback

I want to use autocomplete-fields that link to an external source for their autocomplete-data. Drupal seems to refuse all autocomplete_paths that are not reachable within Drupal. Any ideas how to circumvent that problem? The form field looks like that:
$form['business_city'] = array(
'#type' => 'textfield',
'#size' => 30,
'#title' => t('city'),
'#autocomplete_path' => '_/city?=',
'#default_value' => $userProfile->field_address_business_city[0]['value'],
);
_/city is not reachable within Drupal for performance reasons. The script bootstraps Drupal up to session-level to check for a valid login.
UPDATE:
If I create an autcomplete-field by attaching the needed markup manually to the field it works but it is awkward to maintain:
'#attributes' => array('class' => 'form-autocomplete'),
'#suffix' => '<input type="hidden" disabled="disabled" value="/_/city?n=" id="edit-private-city-autocomplete" class="autocomplete">',
Instead of hacking, you could make sure that the path you are querying "/_/city?n=" is a valid menu_hook item. That way it will validate against the drupal_valid_path() inside theme_textfield(). From within the menu hook function callback you could then forward the request to your external data source.
Drupal 6 validates in theme_textfield() if the autocomplete path is a valid (internal) path.
So, you can't work around this unless you override that theme function.

Make changes to main form with ahah callback in Drupal?

I have a form like poll form.When there is no data in db I want to show only add button and when user clicks "more" I want to show him/her a submit button.
I used the following code but it seems doesn't works.
if ($form['count']['#value'] > 0) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
}
How can I do this?
The values will be in $form_state['values'] so try:
if($form_state['values']['count'] > 0){....
I would expect that at this point the $form['count']['#value'] is not set.

hook_nodeapi() - How to order additional fields

I used hook_nodeapi to add my custom field to a type of node
$node->content['my_new_field'] = array(
'#value' => $content,
);
However the new field is only appeared at the end of the content. Is there anyway for me to select a place for it to display ? e.g: between Title and Body.
For some reason I won't be able to use CCK, I want to do it programmatically.
Thanks in advance
There is something called weight. If you llok at the code from API docs, you'll see how that is supposed to work. Lower numbers appear before higher numbers.
So you could do something like
$node->content['my_new_field'] = array(
'#value' => $content,
'#weight' => 5, //play with the values until you are happy with the output
);
Couldn't you implement hook_load instead to append the node object with your custom fields:
http://api.drupal.org/api/function/hook_load/6
Then you could theme it however you want in node-customtype.tpl.php. Just a thought.

Resources