Symfony Unit Testing - Set Value for Javascript Rendered Elements - symfony

In Unit Testing, How can I set a value to the Select box options in which drop down options are rendered from Javascript ?
When I set a value, I am getting Invalidargument exception.
Note: Form is a general HTML Form
Referred links: symfony unit tests: add/modify form action

Thanks for # Matteo comments.
In Unit testing,
For setting values for the Select box, which are not available in the Drop down,
Use Posting the data instead of submitting the form,
$this->client->request('POST', $postUrl, $formValueArray);
$formValueArray = array('data' => 'value');
or
$formValueArray = array(
'myform' => array(
'data' => 'value'
))
);
Note: It can be used to set all the form fields which are not available in the forms.

Related

Symfony EntityType - access choice_label after submitting

I have an EntityType field:
->add('topic', null, [
'required' => false,
'class' => Category::class
])
And for new Category 's my js tool creates a new select option marked with __new_option__:
So the value to read is not the value. It is the label.
How can I read this value after submission.
I tried addViewTransformer, addEventListener with preSetData and postSetData.
But when I get the value - it shows everytime just __new_option__ but not the value to persist into the database.
Is there a way to do that?
How can I read this value after submission. I tried
addViewTransformer, addEventListener with preSetData and postSetData.
By reason of the post-data containing the topic-field eq 'new_option'. "It doesn't know anything about the selected value`s label".
I don`t know your implementation, but -> just change-modify the js-behaviour on submitting like:
let topicSelection = document.getElementById('SELECTOR');
topicSelection.options[el.selectedIndex].value = topicSelection.options[el.selectedIndex].innerHTML;
//... submitting

How it is possible to show a choice field (checkboxes) with the selected value

I use an Symfony2 form with a choice field based on 4 checkboxes. Now i have the Problem, that i can't reload data in this choice field.That means, when in the database the value 9 is stored, will the checkbox "unknown if "lang test" performed" not be selected, when i load the entity in this form.
->add('fu5LangTest', 'choice', array(
'choices' =>array(
'poor compliance'=>0,
'negative'=>1,
'positive (recognizes at least 1 image reliably)'=>2,
'unknown if "lang test" performed'=>9),
'choice_value' =>function (DataEFu5 $entity = null) {
return $entity ? $entity->getFu5LangTest() : '';
},
'expanded'=>true))
I tried a lot of things with the choice_value Attribut, but i think i'm using it wrong. Whats the right way? i didn't found some useful Information. What i have to do, that the correct checkbox will be selected after an reload?
The storing process is working fine.
Thanks for an feedback
The form is not the right place to do domain Logic.
Anyway, the data has the current fu5LangTest value. You should use
->add('fu5LangTest', 'choice', array(
'choices' =>array(
'poor compliance'=>0,
'negative'=>1,
'positive (recognizes at least 1 image reliably)'=>2,
'unknown if "lang test" performed'=>9
),
// ...
Form creation in Controller:
$object = new DataEFu5(); //or get it by your repository
//this condition will be better in a service as it is business logic ;)
if(!in_array($object->getFu5LangTest, [0,1,2])) {
$object->setFu5LangTest(9);
}
$form = $this->createForm(ObjectType, $object);

Drupal 7 db_insert on WYSIWYG textarea field

this is my first post here at stackoverflow and i'll try make sharp and short ;-)
I am running into problems when inserting a textarea form field using CKeditor into the database.
Here is my form element:
$form['add']['description'] = array(
'#wysiwyg' => true,
'#name' => 'description',
'#title' => t('description'),
'#type' => 'text_format',
'#base_type' => 'textarea',
);
When submitting the form i do get a SQL error like this because Drupal appends the placeholder elements 'value' and 'format'
db_insert failed. Message = SQLSTATE[21S01]:
Insert value list does not match column list:
1136 Column count doesn't match value count at row 1,
query= INSERT INTO {tablename} (description)
VALUES (:db_insert_placeholder_13_value, :db_insert_placeholder_13_format)
Unfortunately i exactly have to set up the textarea this way to make the CKeditor work. Can you advice me how to get rid of the
:db_insert_placeholder_13_value,
:db_insert_placeholder_13_format
to a single variable again?
I really appriciate your help
PROBLEM SOLVED --- silly me, should have used my glasses more :-D
Just use the correct array index inside the submit hook for saving the data to the database and it will work:
function MYMODULE_form_add_submit($form, &$form_state) {
...
'description' => $form_state['values']['description']['value'],
...
}
If you're just interested in the text itself (not the format), add a validation handler for the form and use something like this
function MYMODULE_form_name_validate($form, &$form_state) {
$form_state['values']['description'] = $form_state['values']['description']['value'];
}

Validate a Collection Field Type in Symfony 2 with allowExtraFields=true

I'm trying to validate a collection form field:
$builder->add(
'autor',
'collection',
array(
'type' => 'text',
'options' => array('required' => false),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'error_bubbling' => false
)
);
I use JavaScript, as suggested in the Cookbook, to dynamically add more text fields to the collection. My problem is, that I don't know, how to validate these fields. The collection validator lets me validate specific fields of a collection by their name but not simply every field of it. How can I manage that?
Even cooler would be, if I could check, if at least one of the fields is notBlank instead of forcing it to every field.
Best regards
You can use the "constraints" option defined in form Field Type that are available on all fields.(http://symfony.com/doc/master/reference/forms/types/form.html#constraints).
In your case, you can add your constraint like this:
$builder->add('autor', 'collection', array(
'constraints' => new NotBlank()),
));
(in this case dont forget to include the constraints provided by the Validation component:
use Symfony\Component\Validator\Constraints\NotBlank; ...)
i didnt test but i think with this every input will be validate againts the constraint you assigned to the field, and as you have the option "error_bubbling" as false, an error message should be attached to the invalid element.
--EDIT--
Since you even use the 2.0 version of Symfony i think this solution solves your problem, however I strongly advise you to update to the 2.3 version.
You can create a Form Event Subscriber(http://symfony.com/doc/2.0/cookbook/form/dynamic_form_modification.html) that will be listening the POST_BIND event.(note that Post Bind event is Deprecated since version 2.3 and will be removed in 3.0);
In your subscriber class, you will validate each of your submited authors as you want and add an error to the form if something is wrong.
Your postBind method could be something like this:
public function postBind(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
// get the submited values for author
// author is an array
$author = $form['autor']->getData();
// now iterate over the authors and validate what you want
// if you find any error, you can add a error to the form like this:
$form->addError(new FormError('your error message'));
// now as the form have errors it wont pass on the isValid() method
// on your controller. However i think this error wont appear
// next to your invalid author input but as a form error, but with
// this you can unsure that non of the fields will be blank for example.
}
you can check the Symfony2 Form Component API if you have any doubt about a core method.
http://api.symfony.com/2.0/index.html

Drupal form validation functions

Is there anyway say Drupal to validate form elements like email fields, passwords, numeric fields validate automatically lets say bind a system validator
$form['email] = array(
'#title' => t('Email'),
'#type' => 'textfield',
'#validate_as' => array('email', ...),
...
);
To validate a numeric field in Drupal use:
'#element_validate' => array('element_validate_number')
No need to create a custom validation function.
http://api.drupal.org/api/drupal/includes%21form.inc/function/element_validate_number/7
Rimian is both correct and wrong.
The good thing as Rimian points out, is that you can attach any validation function to your form fields using the #element_validate.
However I'm not aware of a set of form api validation functions you can call to test most common things like, if the value is:
a int
a positive int
a valid date (such a function exists in the date module though)
a email-address (you can use valid_email_address to check the email, but you need a function to raise validation error)
So while you can do this, it's a bit more work than you were hoping for, as you will need to create these validation functions yourself. But once you have done this, you can reuse them with #element_validate.
The use of #element_validate is mostly centered around complex validation fx date validation, location validation and such, as it requires some work to create these validation functions. Most of the time, you don't need to validate that many numbers etc (which you quite easily could do within a normal validation function using a loop). So I'm not sure how much this will be of help to you, but it's definitely a possibility.
The Form API Validation module does exactly what you request: http://drupal.org/project/fapi_validation
For client-side validation there is also http://drupal.org/project/clientside_validation (which can use rules provided by Form API Validation).
Yep!
Though I have not experimented with it much.
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#element_validate
$form = array(
'#type' => 'fieldset',
'#title' => t('Input format'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => $weight,
'#element_validate' => array('filter_form_validate'),
);

Resources