D
D
des1roer2015-05-15 10:03:51
Yii
des1roer, 2015-05-15 10:03:51

Yii CHtml::textField own validation?

textField fields are not in the model. they are from another table. that is, I cannot use validation from the model. but with the request I get what is indicated by the field in the type (float, text, int). How can I check what the user enters.
something like
(real)$value = str_replace(',','.',$value);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
des1roer, 2015-05-18
@des1roer

Not very elegant, of course.
_form.php

echo '<div class="column">';
            echo CHtml::label($rows[$i]['name'], $rows[$i]['name']);
            echo CHtml::textField("elem[$id][val][]", $val, array('id' => $rows[$i]['name']));
            echo '</div>';
            echo '<div class="column" style="display:none">';
            echo CHtml::textField("elem[$id][type][]", $type, array('id' => $rows[$i]['name']));
            echo '</div>';

we get the following html code
<div class="column">
<label for="Вагонов">Вагонов</label>
<input id="Вагонов" type="text" name="elem[5][val][]" value="">
</div>
<div class="column" style="display:none">
<input id="Вагонов" type="text" name="elem[5][type][]" value="int">
</div>

controller
if (isset($_POST['elem']))
                    {
                        $command = Yii::app()->db->createCommand();
                        $max = Yii::app()->db->createCommand()
                                ->select('max(id) as max')
                                ->from('a_analiz_create')
                                ->queryScalar();

                        $Ids = $_POST['elem'];
                         $cnt = max(array_keys($Ids));
                        $min = min(array_keys($Ids));
                        for($i = $min; $i <= $cnt; $i++)
                        {
                            $type = $Ids[$i]['type'][0];
                            $val = $Ids[$i]['val'][0];
                            if ($type == 'float')
                            {
                                (real)$val = str_replace(',','.',$val);                                 
                            }
                            else if ($type == 'int')
                            {
                                $val = (int)$val;                                 
                            }   
                            if (isset($val) && !empty($val))
                                $command->insert('a_analiz_data', array(
                                    'analiz_id' => $max, 
                                    'value' => $val,
                                    'elem_id' => $i,
                                    'user_id' => Yii::app()->user->id,
                                ));
                        }
                    };

M
matperez, 2015-05-15
@matperez

If you need to validate a field from another model in the form, you can create a fake field, and validate it.
When creating a form, load data from another model into it, when saving the form, after validation, upload data from the fake field to the desired model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question