我正在使用symfony 3和twig。我从API获取DNS信息。但我永远不知道会有多少行。所以这里有问题让我们说域www.example.com有3个DNS当我请求获取表单我的控制器我只从表单中获取最后一个DNS。
树枝:
{% if dnsinfo is defined %}
<form class="form-inline" method="post" action="/setdns">
{% if dnsinfo is not empty %}
{% for dns in dnsinfo %}
<div class="row " >
<div class="input-group col-xs-3">
<input type="text" id="dns" name="dns" value="{{ dns.1 }}" class="form-control">
</div>
<div class="col-xs-2 input-group">
<select class="form-control" id="type" name="type" style="width:100%;">
<option value="notselected" selected disabled>* Select a type</option>
<option value="A"{% if dns.2 == 'A' %} selected{% endif %}>A</option>
<option value="AAAA"{% if dns.2 == 'AAAA' %} selected{% endif %}>AAAA</option>
<option value="CNAME"{% if dns.2 == 'CNAME' %} selected{% endif %}>CNAME</option>
<option value="MX"{% if dns.2 == 'MX' %} selected{% endif %}>MX</option>
<option value="SOA"{% if dns.2 == 'SOA' %} selected{% endif %}>SOA</option>
<option value="TXT"{% if dns.2 == 'TXT' %} selected{% endif %}>TXT</option>
<option value="SRV"{% if dns.2 == 'SRV' %} selected{% endif %}>SRV</option>
</select>
</div>
<div class="col-xs-3">
<input type="text" id="content" name="content" value="{{ dns.3 }}" class="form-control">
</div>
<div class="col-xs-1">
<input type="text" id="ttl" name="ttl" value="{{ dns.4 }}" class="form-control">
</div>
<div class="col-xs-3">
<input type="text" id="prio" name="prio" value="{{ dns.5 }}" class="form-control">
</div>
</div>
<hr class="divider" style="">
{% endfor %}
{% endif %}
<input type="submit" class="btn btn-success" value="Save">
<a href="/registereddomain" class="btn btn-secondary">Cancel</a>
</form>
{% endif %}
symfony controller:
$this->pre($request->request->get('dns'));
$API = $this->connect();
$dns = array(
'domain' => $request->request->get('domain'),
'extension' => $request->request->get('ext'),
'dnsdata' => serialize( array(
1 => array(
'name' => $request->request->get('dns'),
'type' => $request->request->get('type'),
'content' => $request->request->get('content'),
'ttl' => $request->request->get('ttl'),
'prio' => $request->request->get('prio')
)
))
);
$API->prepare('dns', $dns);
当然我知道一个解决方案。使用字段name1 name2等后面的数字但是必须有更好的方法来做到这一点吗?我想要的是如果我打印我的请求:
[dns] =&gt; array(包含我在表单中发送的所有DNS的数组)
这就是内部表单数据
https://gyazo.com/ebd558ba7f1ef8e1bfbe86e115bbb1e
正如你所看到的那样,我只需要保留$request->request->get()
所需的所有数据。我只能拿最后一个。
所以我的问题是如何获取所有数据?
vardump和返回所有字段https://gyazo.com/0b9725ba8e298c6f59f343d3d5a548c7
的表单sollution :
我最终使用数字,所以我可以知道从api发送了多少字段。然后循环到所有字段
{% if dnsinfo is defined %}
{% set i=0 %}
<form class="form-inline" method="post" id="form" action="/setdns">
<input value="{{ domain }}" hidden name="domain" id="domain">
<input value="{{ ext }}" hidden name="ext" id="ext">
{% if dnsinfo is not empty %}
{% for dns in dnsinfo %}
<div class="row " >
<div class="input-group col-xs-3">
<input type="text" id="dns{{ i }}" name="dns{{ i }}" value="{{ dns.1 }}" class="form-control">
</div>
<div class="col-xs-2 input-group">
<select class="form-control" id="type{{ i }}" name="type{{ i }}" style="width:100%;">
<option value="notselected" selected disabled>* Select a type</option>
<option value="A"{% if dns.2 == 'A' %} selected{% endif %}>A</option>
<option value="AAAA"{% if dns.2 == 'AAAA' %} selected{% endif %}>AAAA</option>
<option value="CNAME"{% if dns.2 == 'CNAME' %} selected{% endif %}>CNAME</option>
<option value="MX"{% if dns.2 == 'MX' %} selected{% endif %}>MX</option>
<option value="SOA"{% if dns.2 == 'SOA' %} selected{% endif %}>SOA</option>
<option value="TXT"{% if dns.2 == 'TXT' %} selected{% endif %}>TXT</option>
<option value="SRV"{% if dns.2 == 'SRV' %} selected{% endif %}>SRV</option>
</select>
</div>
<div class="col-xs-3">
<input type="text" id="content{{ i }}" name="content{{ i }}" value="{{ dns.3 }}" class="form-control">
</div>
<div class="col-xs-1">
<input type="text" id="ttl{{ i }}" name="ttl{{ i }}" value="{{ dns.4 }}" class="form-control">
</div>
<div class="col-xs-3">
<input type="text" id="prio{{ i }}" name="prio{{ i }}" value="{{ dns.5 }}" class="form-control">
</div>
</div>
<hr class="divider" style="">
{% set i = i + 1 %}
{% endfor %}
{% endif %}
<input type="submit" class="btn btn-success" onclick="$('#form').attr('action','/setdns{{ i }}')" value="Save">
<a href="/registereddomain" class="btn btn-secondary">Cancel</a>
</form>
{% endif %}
正如您在我的按钮中看到的,我使用jquery将数字发送到我的控制器
/**
* @Route("/setdns{i}", name="setdns", requirements={"i": "\d+"})
*/
public function setdns(Request $request, $i = false) {
$API = new Whoisdomainmodify();
$API = $API->connect();
for ($x=0; $x < $i; $x++) {
$dns = array(
'domain' => $request->request->get('domain'),
'extension' => $request->request->get('ext'),
'dnsdata' => serialize(array(
1 => array(
'name' => $request->request->get('dns'.$x),
'type' => $request->request->get('type'.$x),
'content' => $request->request->get('content'.$x),
'ttl' => $request->request->get('ttl'.$x),
'prio' => $request->request->get('prio'.$x)
)
))
);
echo '<pre>';
print_r($dns);
$API->prepare('dns', $dns);
// $API->execute();
// $API->fetch();
}
$this->addFlash('Success', 'DNS successfully changed');
return $this->redirectToRoute('registereddomain');
}
如果有更好的方法,请告诉我。因为我发送所有表格数据,请参阅gyazo。
答案 0 :(得分:2)
您是否知道Twig中的for循环有变量:
http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable
例如,你可以使用:<MenuItem
x:Name="Menu1"
ItemsSource="{Binding ListOfObject1}"
Visibility="{Binding ListOfObject1, Converter={u:NullToVisibleConverter}}"
>
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter
Property="IsEnabled"
Value="true"
/>
<Setter
Property="Header"
Value="{Binding SomePropertyOfItemClass}"
/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition
Binding="{Binding PropertyInViewModel, Converter={baseTc:IsNullConverter}, RelativeSource={RelativeSource AncestorType=MenuItem}}"
Value="true"
/>
<Condition
Binding="{Binding BoolInObject1}"
Value="True"
/>
</MultiDataTrigger.Conditions>
<Setter
Property="IsEnabled"
Value="False"
/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
或类似的东西。试试看。 Twig是非常强大的,如果你熟悉它,它可能会为你节省大量的代码。我不确定这是否是一个答案,但它可能会帮助你。