I'm having issues attempting to POST to a script that requires a query string. I'm using the code below, but I get the same error response I would get if I requested the page without the query string or set the query variable to "".
For example http://example.com/login?validate works in the browser, but http://example.com/login?validate="" does not (page displays error)
My code:
$client = new Client();
$crawler = $client->request("POST","https://www.example.com/cgi-bin/login?validate", array(
'USERNAME' => 'TEST',
'PASSWORD' => 'TEST'
));
Normally you would use the route like this:
$client->request('POST', '/login', array(
'USERNAME' => 'TEST',
'PASSWORD' => 'TEST'
)
);
In your particular case, I don't think you need to add ?validate as it should be triggered when you submit the form:
$client->request('POST', 'https://www.example.com/cgi-bin/login', array(
'USERNAME' => 'TEST',
'PASSWORD' => 'TEST'
)
);
Also, make sure your field is USERNAME and not username, it is case sensitive.
Related
Help, please, there is a task from the front on api to fill out the drupal contact form.
I have a contact form and a method, I need help with the code that will submit the data, I found this solution:
$form_state = new FormState();
$values = [
'message' => 'test',
'name' => 'test'
];
$form_state->setValues($values);
\Drupal::formBuilder()->submitForm('data_portal_contact_us', $form_state);
In the log it gives the following error:
InvalidArgumentException: The form argument data_portal_contact_us is not a valid form. in Drupal\Core\Form\FormBuilder->getFormId() (line 197 of /www/core/lib/Drupal/Core/Form/FormBuilder.php).
Try to use The Form class Name in the $form_arg parameter of submitForm($form_arg, FormStateInterface &$form_state)
$values = [
'message' => 'test',
'name' => 'test'
];
$form_state = (new FormState())->setValues($values);
\Drupal::formBuilder()->submitForm('Drupal\module_name\Form\customModuleNameForm', $form_state);
I'm trying to update the user meta with spaces registered on my website. I am using register_meta hook to register user meta. And for updating users using WordPress REST API I use wp_remote_post but the problem is when I tried to update a user meta with spaces it will add as array in response then it will add as new user meta.
For example the value of user meta is - community test
Sample Response:
[meta] => Array
(
[agent_community] => Array
(
[0] => community
[1] => test
)
)
Sample Code for registering user meta.
register_meta('user', 'agent_community', array(
"type" => "string",
"show_in_rest" => true
));
Update using wp_remote_post
$update_data = array('email'=> 'test#mail.com', 'meta[agent_community]' => 'community test');
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'username:password' ),
),
'body' => array_filter($update_data));
$update_url = 'https://test-url.com/wp-json/wp/v2/users/17';
$response = wp_remote_post( $update_url, $args);
Database:
Is there a way that can merge the array on user meta?
implode() might help you. And you can change spaces with HTML entities. Or use function like urledncode() / urlendecode() or something like this.
I am trying to send a variable in a redirect, but it doesn't work, how can I send a variable with when returning back to the same page?
This is was my attempt:
$player = Player::updateOrCreate(
[
'user_id' => Auth::user()->id,
],
[
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'nationality' => $request->nationality,
'birthday' => $request->birthday,
'club' => $request->club,
]
);
return Redirect::route('admin.articles.index', compact('player'));
//return redirect()->back()->with(['player' => $player]);
If you have something like:
$player = 'player_namne';
return back()->with(compact('player'));
in you function, you have to access it in your view ( or controller ) like that:
#if(session('player'))
{{ session('player') }}
#endif
if you need the value even after refreshing the page then modify like below
$player = 'player_name';
Session::put('plName', $player);
Then when you need it again just call it Session::get('plName');
if you need it temporarily Session::flash('plName', $player);
You also need the use statement use Illuminate\Support\Facades\Session;
I got a form in Symfony but it won't validate and I don't know why.
From UserBundle/Entity/Task.php
/**
*
* #ORM\ManyToMany(targetEntity="Project\TaskBundle\Entity\Task", inversedBy="users")
* #ORM\JoinTable(name="project_users_tasks")
*
*/
protected $tasks;
From UserBundle/Form/CreateUserFormType.php which is my formbuilder:
$builder->add('tasks', 'entity', array(
'class' => 'TaskBundle:Task',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('b')
->andWhere('b.owner = :owner')
->setParameter('owner', $this->securityContext->getToken()->getUser())
->addOrderBy('b.updated', 'ASC');
},
'expanded' => false,
'multiple' => false
));
The post request in my browser:
------WebKitFormBoundary6IBT2Ycy78N9AI7u
Content-Disposition: form-data; name="createUser[tasks][]"
14
The result I get for the form is an error concerning the tasks:
"This value is not valid"
I got no other validation what so ever. So why can't the task 14(which is clean the dishes for example) be assigned to my user? I mean the id of the task should work or not?
Edit:
Symfony doesn't seem to recognize the data, that's why. A print_r of $form->getData();
[tasks:protected] => Doctrine\Common\Collections\ArrayCollection Object
(
[_elements:Doctrine\Common\Collections\ArrayCollection:private] => Array
(
)
)
But how can that be? I can see that my browser is posting the data.
At first glance, there are two brackets too much in your submitted field name: createUser[tasks][] should be createUser[tasks], since the entity field is a collapsed, single-valued entity field.
Also try to debug Symfony's request object. var_dump($request->request->get('createUser')) should return something like
array(
'tasks' => 14,
)
If, on the other hand, you really want to submit one or more tasks, set the "multiple" option on the entity field to true. Then the request data should be something like
array(
'tasks' => array(
0 => 14,
)
)
1) Where is the best place to populate a new database table when a module is first installed, enabled? I need to go and get some data from an external source and want to do it transparently when the user installs/enables my custom module.
I create the schema in {mymodule}_schema(), do drupal_install_schema({tablename}); in hook_install. Then I try to populate the table in hook_enable using drupal_write_record.
I confirmed the table was created, I get no errors when hook_enable executes, but when I query the new table, I get no rows back--it's empty.
Here's one variation of the code I've tried:
/**
* Implementation of hook_schema()
*/
function ncbi_subsites_schema() {
// we know it's MYSQL, so no need to check
$schema['ncbi_subsites_sites'] = array(
'description' => 'The base table for subsites',
'fields' => array(
'site_id' => array(
'description' => 'Primary id for site',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
), // end site_id
'title' => array(
'description' => 'The title of the subsite',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
), //end title field
'url' => array(
'description' => 'The URL of the subsite in Production',
'type' => 'varchar',
'length' => 255,
'default' => '',
), //end url field
), //end fields
'unique keys' => array(
'site_id'=> array('site_id'),
'title' => array('title'),
), //end unique keys
'primary_key' => array('site_id'),
); // end schema
return $schema;
}
Here's hook_install:
function ncbi_subsites_install() {
drupal_install_schema('ncbi_subsites');
}
Here's hook_enable:
function ncbi_subsites_enable() {
drupal_get_schema('ncbi_subsites_site');
// my helper function to get data for table (not shown)
$subsites = ncbi_subsites_get_subsites();
foreach( $subsites as $name=>$attrs ) {
$record = new stdClass();
$record->title = $name;
$record->url = $attrs['homepage'];
drupal_write_record( 'ncbi_subsites_sites', $record );
}
}
Can someone tell me what I'm missing?
If ncbi_subsites_get_subsites() is not in the .install file, you need to include whatever file its in with your module. Otherwise, it's returning nothing, in which case try dumping $subsites and exiting.
I think the answer is that drupal_write_record is not meant for install or enable hooks. I think when enabling or installing, you have to write SQL. That is the impression I am getting from reading some posts that mention that the schema is not available in these hooks.
First of all (assuming Drupal 6), drupal_write_record() cannot be called from hook_install() because Drupal would not find the database schema defined from the module, which is still going to be installed, and enabled.
Instead you need to use db_query() function. (the comments are speaking of a way to include default data by prviding it to hook_schema() serialized, but i've found no documentation on this.)
However, would you be using (the development version of) Drupal 7, you want to look at the db_insert() function instead.