Changing selected option in HTML::Form - html

I am working on an automated form submit script. It is logging in to a vendor's website and populating the fields of a form. When trying to submit, the desired result would be a ticket number displayed, which is acknowledging the form is submitted and the request is processed by their helpdesk.
However the form is not submitted correctly (no acknowledgement is displayed) and I suspect that it is caused by one of the inputs which is a SELECT.
Here is the code I use to set this field:
$forms[3]->value('ProductList','-2');
This has no effect on the the prepared form unfortunately, dumping $forms[3], i see this:
[...]
bless({
'onchange' => ' checkKC(document.all.ProductList, \'~0\'); prodExpand();',
'current' => 1,
'menu' => [
{
'seen' => 1,
'value' => '~0',
'name' => '<Please select>'
},
{
'seen' => 1,
'value' => '-2',
'name' => 'Product not found.... Search more'
},
{
'value' => '-1',
'name' => '------------------------------------'
},
{
'value' => 'Product1',
'name' => 'Product 1 Name'
}
],
'name' => 'ProductList',
'id' => 'ProductList',
'idx' => 1,
'type' => 'option'
}, 'HTML::Form::ListInput' ),
[...]
Am I using the right method of $forms[3]? (it was created by HTML::Form->parse($pageresult) btw) Or is there any other method I should try? I can't find any documentation for HTML::Form::ListInput
Thanks for any advice

Consider using WWW::Mechanize for form processing that takes more than one step. That way you can include the login process in your script along with going to the form and of course getting the result.
Or if you need to work with JavaScript, then use WWW::Mechanize::Firefox.

Related

Symfony form with ChoiceType conditional

I want to create a form in symfony with a double checkbox, in mode You approve the privacy policy or you don't approve the privacy policy.
[]I approve the privacy policy [] I do not approve the privacy policy
The doubt that arises is that one of them will be marked dynamically only if the answer to a query is a value a or another (For example a boolean) (So I think it should be relational), in plan: 1 to approve and 0 to not approve, but previously made the query to generate the form.
What I currently have is the following:
$form = $this->createFormBuilder()
->add('LOPD', ChoiceType::class, [
'multiple' => false,
'expanded' => true,
'choices' => [
"Approve the privacy policy" => "0",
"Do not approve privacy policy" => "1",
],
'data' => '1'
])
Then, I don't know how to configure so that the default value, when loading the form, is taken from the User entity. In other words, if the query to user returns a 1, the default option is 0, and the other way around with 0.
I have searched that you can make a query with query_builder, but it is not clear to me how to put the query to do that:
->add('client', EntityType::class, [
'class' => 'App\Entity\User',
'expanded' => true,
'multiple' => false,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
//->where('u.First_Name', ':uid')
//->setParameter('uid', $this->getUser()->getId())
->orderBy('u.id', 'DESC');
},
/*'choice_label' => 'First_Name',*/
'choice_label' => 'tipo',
'choice_value' => 'id'])
How can I do it?

Pass Data Directly to yii2-tree-manager

I have a tree node in my form. I am using kartik-v's Tree Manager.
This is my view code:
echo TreeViewInput::widget([
'query' => Tree::find()->addOrderBy('root, lft'),
'headingOptions' => ['label' => 'Set Permission'],
'name' => 'name',
'value' => '1,2,3',
'asDropdown' => false,
'multiple' => true,
'fontAwesome' => true,
'rootOptions' => [
'label' => '<i class="fa fa-tree"></i>',
'class' => 'text-success'
]);
But, in this I have to follow the same table structure as mentioned in the widget. I have some extra fields and more permissions. So it is a bit complicated to use the same structure.
Is it possible to pass the value in an array directly to this widget? If possible let me know the array format.
Now I am stuck with this tree node implementation.
You can do this by doing some tricks, or by using another way:
1) you can add a condition to your query like this:
Tree::find()->andWhere(['not in','id',[2,3,4]])->addOrderBy('root, lft'),
by this solution you can ignore unwanted rows like you send data direct in array...
2) you can use another solution by using js lib/plugin direct like jsTree, in this case you can create and pass custom array direct...look at this example: jsTree Example

Saving data to the join table using control options in CakePHP 3.x

I learned here how one can save the data to the fields of join table CoursesMemberships while adding or editing a student in CakePHP 3.x. In order to add grades for many courses I can do this in my add and edit forms:
echo $this->Form->control('courses.0.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.0._joinData.grade');
echo $this->Form->control('courses.1.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.1._joinData.grade');
echo $this->Form->control('courses.2.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.2._joinData.grade');
...
but this form:
has a fixed number of courses for each student;
requires to select the course id from the list ('type' => 'select');
adds all courses to the student record even if not attended (well, the corresponding grade field can be kept empty, but still).
Is there a way to have a simpler form, where all courses are listed and one can only checkbox the course attended and enter the corresponding grade? I found it very challenging using control...
EDIT:
After #ndm suggested a very nice method below, I implemented it in the add.ctp:
foreach ($courses as $key => $course) {
echo $this->Form->control('courses.'.$key.'.id', ['type' => 'checkbox', 'hiddenField' => false, 'value' => $key,
'label' => $key]);
echo $this->Form->control('courses.'.$key.'._joinData.grades');
}
and corrected StudentsTable.php accordingly. And it runs with no problems.
However, if I do the same in edit.ctp, the previously saved records (e.g. for 1, 3, 5 and 7 courses are now listed as 1, 2 and 3 showing the grades for former 3rd 5th and 7th courses and the form forces me to check those three boxes. I understand that the first record disappeared because my courses start with id=1 (and so does the $key in the loop) and 'courses.0.id' is thus missing, but the general problem is that the empty fields removed by beforeMarshal function are no longer recognized in edit.ctp form and I cannot find a reasonable way to edit the student's record.
There is no build in support for what you are trying to achieve, you'll have to come up with a custom solution, which will likely either require a mixture of form and marshalling logic, or JavaScript.
You could create for example a list of checkboxes, and use the id value (wich will be zero in case the checkbox isn't checked, or the ID in case it is checked) to remove unchecked entries from the submitted data before marshalling, something like this:
echo $this->Form->control('courses.0.id', [
'type' => 'checkbox',
'value' => $courses[0]->id,
'label' => $courses[0]->title
]);
echo $this->Form->control('courses.0._joinData.grade');
echo $this->Form->control('courses.1.id', [
'type' => 'checkbox',
'value' => $courses[1]->id,
'label' => $courses[1]->title
]);
echo $this->Form->control('courses.1._joinData.grade');
// ...
// in the `StudentsTable` class
public function beforeMarshal(\Cake\Event\Event $event, \ArrayObject $data, \ArrayObject $options)
{
forach ($data['courses'] as $key => $course) {
if (empty($course['id'])) {
unset($data['courses'][$key])
}
}
}
Alternatively you could use JavaScript to disable the controls related to the checkbox so that they aren't being submitted in the first place. For this to work properly you'll need to make sure that you disable the hidden field that is by default being generated for checkboxes (see the hiddenField option), as otherwise zero will be sent for unchecked checkboxes.
Here's a quick, untested jQuery example to illustrate the principle:
echo $this->Form->control('courses.0.id', [
'class' => 'course-checkbox',
'data-join-data-input' => '#course-join-data-0',
'type' => 'checkbox',
'hiddenField' => false, // no fallback, unchecked boxes aren't being submitted
'value' => $courses[0]->id,
'label' => $courses[0]->title
]);
echo $this->Form->control('courses.0._joinData.grade', [
'id' => 'course-join-data-0',
'disabled' => true
]);
// ...
$('.course-checkbox').each(function () {
var $checkbox = $(this);
var $joinDataInput = $($checkbox.data('join-data-input'));
$checkbox.on('change', function () {
$joinDataInput.prop('disabled', !$checkbox.prop('checked'));
});
});
See also
Cookbook > Database Access & ORM > Saving Data > Modifying Request Data Before Building Entities
Cookbook > Views > Helpers > Form > Creating Select, Checkbox and Radio Controls > Options for Control
Cookbook > Views > Helpers > Form > Creating Select, Checkbox and Radio Controls > Creating Checkboxes

Checking 2 unique codes against the table and then registering the form

I've 2 different tables with 2 columns [code,status] each and the column 'code' contains unique code in integers (392,21,2981,2743,..etc) and they are about 100 in each table while the status tells if these codes were used or not.
I want the form to be only submitted when both of the provided codes from the user match the codes in those 2 tables and have the status '0'
I could create a very simple validation in the controller but that dosen't make much sense to what i just explained
public function formValidationPost(Request $request)
{
$this->validate($request,[
'name' => 'required|min:5|max:35',
'email' => 'required|email|unique:users',
'mobile' => 'required|numeric',
'code_a' => 'bail|required|exists:code_a,code',
'code_b' => 'bail|required|exists:code_b,code'
],[
'name.required' => ' The name field is required.',
'name.min' => ' The name must be at least 5 characters.',
'name.max' => ' The name may not be greater than 35 characters.',
]);
dd('You successfully added all fields.');
}
So with my validation rules I want to be able to make sure that:
Form doesn't submit unless both the provided codes are matched in the database ( code_a[table] and code_b[table] ) and their status to be '0'
I hope this makes sense.
Thanks
Reference
use Illuminate\Validation\Rule;
$this->validate($request,
[...
'mobile' => 'required|numeric',
'code_a' => [
'bail',
'required',
Rule::exists('code_a', 'code')->where(function($query) {
$query->where('status', 0);
})
],
'code_b' => [
'bail',
'required',
Rule::exists('code_b', 'code')->where(function($query) {
$query->where('status', 0);
})
]
],
[...]);

Magento Admin Create and Save HTML to Database With a Form Field

I am working on a module that requires some html to be entered to be later called upon and become part of a customer facing widget output.
I've created an administrative backend and that is all working properly, however when I enter html into the field that should be storing the data i receive an error.
I dont need the wysiwyg but I would like to be able to enter html into this value.
At this point I've not done anything special when adding the field to the fieldset. What am I missing?
$contentField = $fieldset->addField('inner_html', 'editor', array(
'name' => 'inner_html',
'style' => 'height:36em;width:36em',
'required' => false,
));
Try
$fieldset->addField('inner_html', 'editor', array(
'name' => 'inner_html',
'label' => Mage::helper('tag')->__('Description'),
'title' => Mage::helper('tag')->__('Description'),
'style' => 'width:700px; height:350px;',
'config' => Mage::getSingleton('cms/wysiwyg_config')->getConfig(array('add_variables' => false, 'add_widgets' => false,'files_browser_window_url'=>$this->getBaseUrl().'admin/cms_wysiwyg_images/index/')),
'wysiwyg' => true,
'required' => false,
));