0

I have a field set as required in the model, yet I have seen users saving it as an empty string (ie. ''). When I tested it, I do receive the "Cannot be blank" message properly, so don't know how to prevent this in the future. Do I have to specify all scenarios in the rule (eg, 'insert', 'update')? By the way, I tried updating the field, and it doesn't let me save it empty (I even tries spaces).

These are the rules applied on the field (model):

public function rules()
{
    return array(
        array('field', 'required'),
        array('field', 'length', 'max'=>4096),
        array('field', 'safe', 'on'=>'search'),
    );
}

For @RiggsFolly :) the controller action:

public function actionUpdate($id)
{
    $model = Model::model()->findByPk($id);

    $formData = Yii::app()->request->getPost('Model'); 

    if ($formData)
    {

        $model->attributes = $formData;
        $model->save(); 
    }

    $this->render('update',array(
        'model'=>$model
     ));
}

... and the view:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'form'
)); ?>

<?php echo $form->textArea($model,'text',array( 'rows'=>5 ')); ?>

<?php $this->endWidget(); ?>

Can you imagine any scenario this field could be saving an empty string in the database?

benomatis
  • 5,536
  • 7
  • 36
  • 59
  • Do you actually use this rules method anywhere. On its own its just noise – RiggsFolly Nov 10 '16 at 21:08
  • @RiggsFolly I don't understand your question... – benomatis Nov 10 '16 at 21:09
  • The code you show does not do anything unless it is called somewhere! – RiggsFolly Nov 10 '16 at 21:14
  • @RiggsFolly Seriously? This is not some basic error like I can't put a model, a controller and a view together, it's a strange exception, and I'm looking for ideas to test, not a correction of a dot or a comma somewhere. But so your apparent perfectionism is satisfied, let me add a brief of the controller and view... – benomatis Nov 10 '16 at 21:28
  • @RiggsFolly done... better? – benomatis Nov 10 '16 at 21:37
  • I admit I am no YII Expert, but dont you have to call `$model->validate()` somewhere to get the validation to actually execute? – RiggsFolly Nov 10 '16 at 21:43
  • @RiggsFolly not needed, only if you need a JSON response of the error messages ([see details here](http://www.yiiframework.com/doc/api/1.1/CActiveForm#validate-detail)) – benomatis Nov 10 '16 at 21:46
  • @webeno have you tried with validate? – MackProgramsAlot Nov 10 '16 at 22:06
  • @MackProgramsAlot Have I tried what with validate? the validations works just fine, it does give me the error message, here I'm asking about exceptions, whatever a user can possibly dp to get through the `required` validation and post a form field totally empty - I don't understand how some of these turn up in my db – benomatis Nov 10 '16 at 22:36
  • @webeno I see what you're saying. Very odd. Have you checked all areas of source code (outside of what we see above) that use this model? Does the column have a default value? – MackProgramsAlot Nov 10 '16 at 23:05
  • @MackProgramsAlot I'd have to look through several places, but I hoped there was something easy Inmay have missed, like forcing allowEmpty to be false, but I understand that's not needed if the field is required... the columns has default value null, but even setting that to not null would allow to save empty values (tested that) – benomatis Nov 10 '16 at 23:09
  • I would right click --> find usages on the Model's Class Name, then find anywhere i see ->save() being called.... Sorry I couldn't be of any extra help. Update me if you figure out. – MackProgramsAlot Nov 10 '16 at 23:27
  • @MackProgramsAlot Thanks, will give it a try – benomatis Nov 11 '16 at 08:44

0 Answers0