我最近阅读了这篇post关于如何在没有表单的情况下进行CakePHP 3.x POST(在Index.ctp页面上进行更新,而不是使用Add.ctp或Edit.ctp)。
我有两个问题:
我的index.ctp如下:
<?php foreach ($resources as $resource): ?>
<tr>
<td><?= $this->Number->format($resource->id) ?></td>
<td><?= h($resource->brand) ?></td>
<td><?= h($resource->model) ?></td>
<td><?= h($resource->subtype) ?></td>
<td><?= $this->Form->postButton('Change Type',['controller'=>'Users', 'action'=>'change_resource', $resource->id, $resource->type->name])?></td>
<td><?= $resource->has('studio') ? $this->Html->link($resource->studio->name, ['controller' => 'Studios', 'action' => 'view', $resource->studio->id]) : '' ?></td>
<td><?= h($resource->created) ?></td>
<td><?= h($resource->modified) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $resource->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $resource->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $resource->id], ['confirm' => __('Are you sure you want to delete # {0}?', $resource->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
然后在相应的Controller中链接的StackOverflow帖子中描述的操作:
public function change_resource($id,$existing_type)
{
$resources = TableRegistry::get('Resources');
$resource = $resources->get($id);
$resource->type = ($existing_type == '')?'$resource->$type->name';
$resources->save($resource);
return $this->redirect($this->referer());
}
目前我的语法错误为
意外&#39;;&#39;在第124行,
指的是Controller方法中的$ resource-&gt;类型行。如果我删除该行,我会得到:
第125行的意外$资源
我期望给予;被删除所以它认为他们都在同一条线上。
我希望能够为整个表做一个POST,而不仅仅是一个输入。但是,虽然其中2个输入是从数组形式的其他表中选择的,但其余的都是标准输入(主要是varchar)。我想知道如何修改上面的控制器来做到这一点。另外,如果我想为多个输入做POST,我是否需要为每个输入单独的方法,或者我可以将它全部放在一个方法中吗?
答案 0 :(得分:0)
请注意,您的三元运算符语法不正确。你错过了冒号(:)
语法:(expr1)? (expr2):( expr3)
class ViewController: UIViewController {
@IBOutlet weak var textfield: UITextField!
let mask = "##/##"
override func viewDidLoad() {
super.viewDidLoad()
textfield.delegate = self
}
}
extension ViewController: UITextFieldDelegate {
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
guard let normalText = textField.text else { return false }
let beginning = textField.beginningOfDocument
// save cursor location
let cursorLocation = textField.positionFromPosition(beginning, offset: range.location + string.characters.count)
let newString = (normalText as NSString).stringByReplacingCharactersInRange(range, withString: string)
let newStringClean = newString.stringWithOnlyNumbers().withMask(mask)
guard newString != newStringClean else { return true }
textField.text = newStringClean
guard string != "" else { return false }
// fix cursor location after changing textfield.text
if let cL = cursorLocation {
let textRange = textField.textRangeFromPosition(cL, toPosition: cL)
textField.selectedTextRange = textRange
}
return false
}
}
extension String {
func stringWithOnlyNumbers() -> String {
return self.characters.reduce("") { (acc, c) -> String in
guard c.isDigit() else { return acc }
return "\(acc)\(c)"
}
}
func withMask(mask: String) -> String {
var resultString = String()
let chars = self.characters
let maskChars = mask.characters
var stringIndex = chars.startIndex
var maskIndex = mask.startIndex
while stringIndex < chars.endIndex && maskIndex < maskChars.endIndex {
if (maskChars[maskIndex] == "#") {
resultString.append(chars[stringIndex])
stringIndex = stringIndex.successor()
} else {
resultString.append(maskChars[maskIndex])
}
maskIndex = maskIndex.successor()
}
return resultString
}
}
extension Character {
func isDigit() -> Bool {
let s = String(self).unicodeScalars
let uni = s[s.startIndex]
let digits = NSCharacterSet.decimalDigitCharacterSet()
let isADigit = digits.longCharacterIsMember(uni.value)
return isADigit
}
}