我写了一个函数,我通过了所有的params来添加用户。
public function userAddAction()
{
$this->requirePostParams(['username', 'usernameCanonical', 'email', 'emailCanonical', 'password', 'firstName', 'lastName', 'birthDay',
'addressOne', 'addressTwo', 'city', 'country', 'postCode', 'mobile', 'about']);
$this->get('user')->addNewUser($this->data['username'], $this->data['usernameCanonical'], $this->data['email'],
$this->data['password'], $this->data['emailCanonical'], $this->data['firstName'],
$this->data['lastName'], $this->data['birthDay'], $this->data['addressOne'],
$this->data['addressTwo'], $this->data['city'], $this->data['country'], $this->data['postCode'],
$this->data['mobile'], $this->data['about']);
return $this->success();
}
protected function requirePostParams($params)
{
$currentRequest = $this->get('request_stack')->getCurrentRequest();
$postData = $currentRequest->request->all();
$postContent = json_decode($currentRequest->getContent(), true);
if (!empty($postContent)) {
$postData = $postContent;
}
$this->data = $postData;
$missingParams = [];
foreach ($params as $param) {
if (!array_key_exists($param, $postData)) {
$missingParams[] = $param;
}
}
if (!empty($missingParams)) {
$this->terminateWithResponse($this->jsonError(sprintf("Missing params: %s", join(', ', $missingParams))));
}
}
现在,我想将所有$ this->数据....打包到一个调用中,以减少我需要输入的所有字段..
答案 0 :(得分:0)
您应该可以在这里使用Symfony Forms。
通常你需要调用类似的东西:
handleRequest
但据我所知,你的请求中有json字符串。因此,在设置$this->data
后,您需要拨打$form->submit($this->data);
try
{
using(var con = new SqlConnection(conString))
using(var cmd = new SqlCommand("insert into adjustments_config(document_number)values(@docNumber)", con)
{
con.Open();
cmd.Parameters.AddWithValue("@docNumber", TextBoxDocNo.Text);
var rowsAffected = cmd.ExecuteNonQuery();
// ..validate rows affected
Response.Redirect("About.aspx");
}
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
。
您可以查看此方法HttpFoundationRequestHandler::handleRequest,它可以帮助您更好地了解表单实际上如何与请求一起使用。