Reputation: 344
I'm a beginner in cakephp, help me please. I have stuck when I think to validate input type selected in form cakephp, the data is detail, so many rows there, first line.
<select id="AirWayBillDetail0ItemId" name="data[AirWayBillDetail][0][item_id]">
<option value="">-- Select an Item --</option>
<option value="9">Handphone</option>
<option value="10">Accecoris</option>
<option value="11">Alat Tulis Kantor</option>
<option value="12">Voucher Fisik</option>
</select>
I want the option, just one time can be used.
Example: Add first line, I select handphone, so, the second line, cannot used that option. Is there any way to clear my trouble?
Can I validate in Model or in View or Controller? With javascript or jquery?
Upvotes: 0
Views: 173
Reputation: 8540
You want to check this in the model or controller layers. Never rely on JavaScript for validation as this can lead to vulnerabilities in your app (JavaScript can easily be disabled). You should always validate on the server-side and can supplement this with client-side validation if you want some additional inline validation.
You want to add a check that there are no duplicate item_id
in your submitted data. So you could do something like the following in your controller:-
$itemIds = Hash::extract($this->request->data, 'AirWayBillDetail.{n}.item_id');
if (count($itemIds) !== count(array_unique($itemIds))) {
// Invalid item_ids
}
You'd then need to handle how you'd want the error messages showing and ensure it doesn't proceed to save. If you want to do this in the model you need to look at using the beforeSave()
callback to check $this->data
before saving and return false
if duplicated are identified.
Upvotes: 1
Reputation: 1265
You can implement this with jQuery.
Please refer this fiddle http://jsfiddle.net/sqy1n6n3/1/
$('select').on('change', function() {
$('select').find('option').prop('disabled', false);
$('select').each(function() {
$('select').not(this).find('option[value="' + this.value + '"]').prop('disabled', true);
});
});
Upvotes: 1