symfony1 multiple checkboxes require all - checkbox

I want to require all checkboxes in the set
My code looks like this:
$this->widgetSchema['consent_confirmation'] = new sfWidgetFormSelectCheckbox(
array(
'choices' => Doctrine_Core::getTable('MyTable')->getOptions(),
)
);
UPDATE:
My validation looks like this:
$this->validatorSchema['consent_confirmation'] = new sfValidatorChoice(array(
'choices' => array(Doctrine_Core::getTable('MyTable')->getOptions()),
'multiple' => true,
'required' => true
));
How can I make it return 'Required' if they're not all checked, and be valid if they're all checked?

My symfony 1.* memory is very hazy at this point but I think what you need to do here is add a rule to the validatorSchema to handle validation of this widget.
According to the Validation Appendix the validator you need is sfValidatorChoice.
This widget has a number of options, including:
multiple
min
max
Assuming that you have two options as above, and you want to enforce selecting both, I'm guessing that you might need to add the following to your form's configure() method:
public function configure()
{
$this->widgetSchema['consent_confirmation'] = new sfWidgetFormSelectCheckbox(array(
'choices' => array(
'1' => 'Yes I agree to #1',
'2' => 'Yes I agree to #2',
)),
);
$this->validatorSchema['consent_confirmation'] = new sfValidatorChoice(array(
'multiple' => true,
'min' => 2,
'max' => 2,
));
}
Something like that - I'm not sure about the assignment to the validatorSchema to be honest, there might be something like addValidator() or setValidator()methods instead. EDIT: I think there were some helper methods added, but some of these might be 1.4 specific. The above assignment should work either way...
Hope this helps :)

Related

Expression Engine 3: Fieldtype settings - checkboxes

I'm working in Expression Engine 3 and I'm working on a custom field type, specifically the options for said field type. I've been following the docs along with how to build out the form for the settings, and everything is rendering correctly.
My question is with regard to checkboxes (and radio buttons), how do I determine which boxes should be pre-selected based on the currently saved settings?
The code I have for outputting the checkboxes is as follows:
$settings = array(
array(
'title' => 'Select your option(s):',
'fields' => array(
'multi' => array(
'type' => 'checkbox',
'choices' => array(
'1' => 'Option 1',
'2' => 'Option 2'
)
)
)
),
Now, in the display_settings($data) function, if I do a var_dump($data) I can see the values being correctly pulled in. But the checkboxes will not automatically set themselves to selected if they should be.
Any advice would be lovely, thank you! Also, I'd be happy to provide more specific details if needed.

Paginate and Sort bug(s)?

In my controller I have my pagination set to order by 2 fields.
public $paginate = [
'limit' => 50,
'order' => ['first_name', 'last_name']
];
$contacts = $this->paginate($this->Contacts);
This works fine on the first page, but since I left out the default direction => 'ASC' the Paginator links don't work at all:
/contacts?page=2&sort=0&direction=first_name
When I add in the direction, it works, but of course only sorts by the first field, messing up the sort order.
/contacts?page=2&sort=Contacts.first_name&direction=ASC
Should the default direction be explicitly required?
Is there a method to maintain both fields for sorting during pagination?
Sorting by virtual fields (e.g. full_name => first_name . ' ' . last_name) doesn't work as it did in 2.x
Solved both issues with the following:
Set the default sort order to be the same as the virtual field:
public $paginate = [
'order' => ['first_name', 'last_name']
];
Then just add the following to the View to prevent the paginator from overriding the default order unless specified by the user:
if (empty($_GET['direction'])) { $this->Paginator->options(['url' => ['direction' => null, 'sort' => null]]); }

cakephp ->set() a value in an object

I can use the cakephp set() to add a value in an object at the top level but I also need to set a value for an object inside the object and I can seem to access it. Is this possible?
I need to add business_id inside the employee object.
I though I might use $user->set->employee('business_id', '1'); but I get an error on the employee part.
object(App\Model\Entity\User) {
'email' => 'dfgdfg#sdfsdf.com',
'new_password' => 'ttt',
'confirm_password' => 'ttt',
'employee' => object(App\Model\Entity\Employee) {
'name' => 'dsfsfsdfsfd',
'email' => 'sdfsdfsdf#sdfsdf.com',
'surname' => 'sdfsdfsdfsdf',
'employee_num' => 'sdfsdfsdfsd',
'[new]' => true,
I tried it a couple of different ways and I got it work by $user->employee->set('business_id', '1');

How to use empty and selected in same dropdown box in cake php

I'm editing a dropdown box. i need to use empty and selected in same dropdown.
here is my code
$this->Form->input('per', array('id'=>'per','class'=>'inputs con_field','label'=>'per :', 'type'=>'select','options'=>$per_values,'selected'=>$labrcfps['Labourcfps']['per'],'empty'=>'- - Select --'));
If i use empty it automatically takes the empty as selected value.
I'm new in cakephp but i do this with array and parameter defaults :)
like that:
$options = array(
'x' => 'Select',
1 => 'Something',
2 => 'Car',
3 => 'Train'
);
echo $this->Form->input('name', array(
'options' => $options,
'default' => 'x'
));
Maybe that will be working fine :)
Variable $options is just your options after that you can validate if name of that field has value integer or 'x'. If 'x' then deny rest of logical code

How to change the separators for Cakephp Form Helper for multiple checkboxes

I'm generating a bunch of checkboxes through the Form Helper.
Essenciatially I have an array with like $tests = array
$tests = array(1 => 'test', 15=>'test2');
Then I can use it like this
echo $this->Form->input('test_id', array(
'type' => 'select',
'multiple' => 'checkbox',
'div' => false,
'before' => '<li>',
'after' => '</li>',
'separator' => '</li> <li>'));
I expected it would use the div => false to take off the div of every checkbox but it only applies the options to the exterior block. Is there anyway to change all the blocks from <div class=>'checkbox'> to <li class='anything else'>
Just look at the fields it produces, then write your own simple foreach() loop and write them yourself in whatever wrapping element(s) you want.
I forget whether it's possible with Cake or not, but don't think it is. The above is what we've done before - because it's simple to write, it took us less time to write than it did to look further into it :)

Resources