Validate array fields in a form with Codeigniter 2 - codeigniter

I´m looking to find a nice solution for checking a form field which is submitted as array. The key of the field is already set, so it seems declaring the fieldname as suggested in the manual does not work.
Here is the example (HTML Code):
<select name="image_status[366]" id="image_status[366]" class="form-control">
<option value="1">Status 1</option>
<option value="2">Status 2</option>
<option value="3">Status 3</option>
<option value="4" selected="selected">Status 4</option>
</select>
The key of the element (here it´s 366) is created dynamically and is a database key. Now, if I add the following validation rule, it´s not checked by Codeigniter:
array(
'field' => 'image_status[]',
'label' => 'Status',
'rules' => 'required'
)
If I use the following rule (which will not work unless I create this rules dynamically which is quite ugly), the validation works:
array(
'field' => 'image_status[366]',
'label' => 'Status',
'rules' => 'required'
)
Has anybody an idea, how I can define the validation rules, so that it works for an array with a defined index / key?
Thanks a lot in advance,
Michael

make it as a for loop if you are getting selection lists dynamically. do code like following this will create validation dynamically
foreach ($selection_list as $key) {
$this->form_validation->set_rules($key."[]" ,$key, "required");
}
}

Related

How validation Select Option with extra field ( --please select option--)

{!! Form::select('occupation', getOccupationStatus() , null, ["class" =>
"form-control"])!!}
<small class="text-danger">{{ $errors->first('status') }}</small>
How validation this field with please select option
You need to give a name to your select field ( in your case it's occupation I think ), then use Validation Rules as below
// Validation rules ...
$rules = [
...
'occupation' => 'required',
...
];

Map form field to eloquent model with different name

I'm wondering how to map form field to eloquent model. Thing is that form input field has different name than eloquent model.
This is what i have
Model
class Message extends Model
{
protected $fillable = [
'name', 'email', 'subject_id',
];
}
Form
<form action="{{ action('MessageController#store') }}" method="post">
<input id="name" name="name" type="text">
<input id="email" name="email" type="text">
<select id="subject" name="subject">
#foreach ($subjects as $subject)
<option value="{{ $subject->id }}">{{ $subject->title}}</option>
#endforeach
</select>
</form>
Controller
public function store(Request $request)
{
$message = $this->validate(request(), [
'name' => 'required',
'email' => 'required',
'subject' => 'required',
]);
Message::create($message);
}
Notice that form select field name is subject and Message model field is subject_id.
Is it possible to map these two fields in Message model?
I guess it's possible in controller with something like
Message::create([
'name' => $request->input('name');
'email' => $request->input('email');
'subject_id' => $request->input('subject');
]);
but that's not what i want.
I don't expect some code improvements or suggestions as i'm complete Laravel noob :)
You could add a mutator on the Message model.
public function setSubjectAttribute($value) {
$this->attributes['subject_id'] = $value;
}
That essentially tells eloquent there is a subject attribute on the model but under the hood you're modifying subject_id

How to validate form field with array values[] in laravel?

I'm trying to validate form field with array value.
$this->validate($request, [
'includes' => 'required',
'excludes' => 'required'
]);
HTML is as follows:
<input type="text" name="excludes[]" id="package_exclude"
class="form-control">
<input type="text" name="includes[]" id="package_include"
class="form-control">
The outputted value of $request->includes gives ["Include Value One","Include Value Two"] which is working fine..
But the validation doesn't works..
I suppose you want the arrays to have at least one value? In this case, you can use the min validation rule:
$this->validate($request, [
'includes' => 'required|min:1',
'excludes' => 'required|min:1'
]);

Displaying form elements obtained from database

I have a questions table which has a variety of questions of different input types. The format in my seeder is like so
DB::table('questions')->insert([
'name' => 'name',
'type' => 'text',
'text' => 'Name',
]);
DB::table('questions')->insert([
'name' => 'title',
'type' => 'select',
'text' => 'Title',
'values' => serialize(['Mr', 'Mrs', 'Ms']),
'class' => 'selectpicker'
]);
So you can see the above I have one text input and one select, which has serialized list of values. Now within my controller I get the Questions and pass it to my view.
Within my view, I am doing something like the following
#foreach($questions as $q)
<div class="col-xs-12">
<input type="{{ $q["type"] }}"
class="form-control {{ $q["class"] }}"
id="{{ $q["name"] }}"
name="questions[{{ $q["id"] }}]"
>
</div>
#endforeach
Where I am having difficulty is with the select inputs. How would I go about displaying my selects along with their options (values)?
Thanks
To create a list you can check the type of $q in your foreach. The code inside your loop would look like this:
#if( $q['type'] === 'select' )
<select name="questions[{{ $q['id'] }}]">
#foreach( unserialize($q['values']) as $v )
<option value="{{ $v }}">{{ $v }}</option>
#endforeach
</select>
#endif

Input file is accused to be empty by CodeIgniter form_validation

I have an input for file uploading that has required rule, but even when selecting one, the framework accuses it to be empty and throws the error. Below is the rules:
$config = array(
'registro_fisica' => array(
array(
'field' => 'imagem',
'label' => 'IMAGEM',
'rules' => 'required'
),
...
Below, the form:
<form action="/auto/usuario/add" method="POST" name="fisica_1" id="formulario_fisica_1" class="form_registro" enctype="multipart/form-data">
<input type="hidden" name="tipo_usuario" value="F"/>
<div class="p100">
<div class="left">
<span class="titulo">Dados Pessoais e Foto</span>
<input type="file" name="imagem" id="imagem"/>
...
That's it. Even I have chose a file, an error is thrown.
Required rule doesn't seem to work with files, use callback instead. Take a look at http://keighl.com/post/codeigniter-file-upload-validation/

Resources